GMail API Emails Bouncing

我与影子孤独终老i 提交于 2019-11-27 07:43:46

问题


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 <nobody@gmail.com>

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

回答1:


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




回答2:


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("=", "");
}


来源:https://stackoverflow.com/questions/28122074/gmail-api-emails-bouncing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!