Using GMail API in .Net. Creating messaging using Net.Mail.MailMessage. Then using MimeKit to create MimeMessage (using this to send attachment + HTML message). Passing MimeMessage.ToString to Base64 encoder. No API error. Code runs through ok. I can see the message in the sent page in GMail. Mail looks perfect (and the send actually return message id). But then there is the following appended message to this mail in Gmail.
Bounce <[email protected]>
An error occurred. Your message was not sent.
As usual, no other information from Google. How to fix this?
Dim msg = New Net.Mail.MailMessage
msg.Subject = subject
msg.To.Add(New MailAddress(ToEmail))
msg.From = New MailAddress(FromEmail, SenderName)
msg.ReplyTo = New MailAddress(FromEmail, SenderName)
msg.Body = bodyText
msg.IsBodyHtml = True
If Not String.IsNullOrWhiteSpace(fileAttachment) Then
If System.IO.File.Exists(fileAttachment) Then
Dim Attachment As New Net.Mail.Attachment(fileAttachment, "application/pdf")
msg.Attachments.Add(Attachment)
End If
End If
Dim message As MimeMessage = MimeMessage.CreateFromMailMessage(msg)
Dim newMsg = New Google.Apis.Gmail.v1.Data.Message()
newMsg.Raw = Base64UrlEncode(message.ToString)
GmailService.Users.Messages.Send(newMsg, "me").Execute()
Private Function Base64UrlEncode(ByVal input As String) As String
Dim inputBytes = System.Text.Encoding.UTF8.GetBytes(input)
'Special "url-safe" base64 encode.
Return Convert.ToBase64String(inputBytes).Replace("+", "-").Replace("/", "_").Replace("=", "")
End Function
This is the return message. As you can see everything looks ok. Working with Google APIs is the most frustrating thing.
200 OK
- Hide headers -
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-length: 85
content-type: application/json; charset=UTF-8
date: Sat, 24 Jan 2015 05:57:21 GMT
etag: "96Z6JVARoyR8skov3RseF4DCFpA/mFWFskkdSFxyjIhRJHJuhDCBvfY"
expires: Fri, 01 Jan 1990 00:00:00 GMT
pragma: no-cache
server: GSE
vary: Origin, X-Origin
{
"id": "14b1a841e4fff910",
"threadId": "14b1a841e4fff910",
"labelIds": [
"SENT"
]
}
Why your message bounced. Your recipient's address might not work or exist anymore. Or, you might've entered it with a typo.
You should receive a bounce message from Gmail, exactly like when you are sending messages to erroneous email addresses through the standard Gmail UI. When this happens to a small amount of your recipients, it probably means that their addresses are not valid anymore. Best would be to simply clean your database.
Use the Gmail search tool to search for “from:[email protected]”, and then search. This will show all of the bounce notifications you've received.
This is crazy. This was the issue.
This line
msg.ReplyTo = New MailAddress(FromEmail, SenderName)
for whatever reasons (I guess when FromEmail and ReplyTo are same emails) leaves the RFC2822 Reply-To
parameter blank. The parameter remains blank even when msg.ReplyTo
is commented. Needless to say GMail API seems to have issues with Reply-To being left blank. Most definitely a programming bug.
So I had to do this hack in the final RFC2882 message.
inputTxt = Replace(inputTxt, "Reply-To:", "Reply-To: " & FromEmail)
It works now.
********* as pointed out in the comment below, you can use MailMessage.ReplyToList.Add()
instead to solve this issue. So the issue here is that ReplyTo
address is required in Gmail API (even though one might assume that ReplyTo
should default to From
email). **********
Just in case somebody stumbles across this and wants a C# answer, here is what I was able to get working using the previous answer as a guide, with MimeKit.
public void SendEmail(MyInternalSystemEmailMessage email)
{
var mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress);
mailMessage.To.Add(email.ToRecipients);
mailMessage.ReplyToList.Add(email.FromAddress);
mailMessage.Subject = email.Subject;
mailMessage.Body = email.Body;
mailMessage.IsBodyHtml = email.IsHtml;
foreach (System.Net.Mail.Attachment attachment in email.Attachments)
{
mailMessage.Attachments.Add(attachment);
}
var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);
var gmailMessage = new Google.Apis.Gmail.v1.Data.Message {
Raw = Encode(mimeMessage.ToString())
};
Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, ServiceEmail);
request.Execute();
}
public static string Encode(string text)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(bytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With