GMail API Emails Bouncing

Allen King picture Allen King · Jan 24, 2015 · Viewed 13.7k times · Source

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"
 ]
}

Answer

Allen King picture Allen King · Jan 24, 2015

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). **********