First, I apologize for my bad writing of English.
I'm new to the Gmail API, and I'm currently blocked from sending an email with my default signature from my Gmail account.
Here is a snippet of my code:
Dim MySmtp As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
Dim MyMail As MailMessage = New MailMessage()
Try
'***********************
MySmtp.UseDefaultCredentials = False
MySmtp.DeliveryMethod = SmtpDeliveryMethod.Network
MySmtp.EnableSsl = True
MySmtp.Credentials = New NetworkCredential("[email protected]", "XXXX")
MyMail.From = New MailAddress("[email protected]", "XXXXXXXXXXXXX")
MyMail.To.Add("[email protected]")
MyMail.IsBodyHtml = True
MyMail.Subject = "test"
MyMail.Body = "test body"
' MySmtp.SendAsync(MyMail, "tes")
MySmtp.Send(MyMail)
'****************
Catch ex As Exception
End Try
Unfortunately, the email sent does not include my signature by default.
I understood that the signature was attached to the web account of the email address and not to the delegate email.
But how can I add my signature to this email?
Thank you in advance for your help
CodePudding user response:
To add your signature from Gmail, you will need to use the Gmail API
to send the email instead of the SmtpClient
class.
Here is an example for you:
// Create a new Gmail service and authenticate with the Gmail API
var gmailService = new GmailService();
gmailService.Authenticate("[email protected]", "XXXX");
// Create a new email message
var message = new Message
{
Subject = "test",
Payload = new MessagePart
{
// Set the body of the email and include the default Gmail signature
Body = new Body
{
Data = "test body\n\n" gmailService.Users.Settings.SendAs.Get("me", "[email protected]").Signature,
Size = Encoding.UTF8.GetByteCount("test body\n\n" gmailService.Users.Settings.SendAs.Get("me", "[email protected]").Signature)
}
},
// Set the recipient(s) of the email
To = new List<string>
{
"[email protected]"
}
};
// Send the email
gmailService.Users.Messages.Send(message, "me").Execute();
the GmailService.Users.Settings.SendAs.Get
method is used to retrieve the default signature for the Google account. This signature is then included in the body of the email using the Message.Payload.Body property
.
For more info, please refer to https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/
CodePudding user response:
Specify the contents of the email in raw
property. It should be base64 encoded property.
Dim sender As String = "[email protected]"
Dim recipient As String = "[email protected]"
' email message, including the signature
Dim message As String = "From: " sender vbCrLf _
"To: " recipient vbCrLf _
"Subject: Test Email" vbCrLf _
vbCrLf _
"This is a test email with a signature." vbCrLf _
vbCrLf _
"Sincerely," vbCrLf _
"Sender" vbCrLf _
"Sender's Signature"
' Encode in base64
Dim base64Message As String = Base64Encode(message)
Dim requestBody As New With {
.raw = base64Message
}
Dim response = GmailApi.Messages.Send(requestBody)