GMail API电子邮件退回 [英] GMail API Emails Bouncing

查看:90
本文介绍了GMail API电子邮件退回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.Net中使用GMail API.使用Net.Mail.MailMessage创建消息传递.然后使用MimeKit创建MimeMessage(使用它发送附件+ HTML消息).将MimeMessage.ToString传递给Base64编码器.没有API错误.代码运行正常.我可以在GMail的已发送页面中看到该消息.邮件看起来很完美(发送实际上返回了邮件ID).但是在Gmail中,此邮件有以下附加消息.

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.

与往常一样,没有来自Google的其他信息.该如何解决?

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

这是返回消息.如您所见,一切看起来都还不错.使用Google API是最令人沮丧的事情.

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

推荐答案

这太疯狂了.这就是问题所在.

This is crazy. This was the issue.

此行

msg.ReplyTo = New MailAddress(FromEmail, SenderName)

无论出于何种原因(我想FromMmail和ReplyTo都是相同的电子邮件),都将RFC2822 Reply-To参数留空.即使在注释msg.ReplyTo时,该参数仍为空白.不用说,GMail API似乎将Reply-To留为空白.绝对是一个编程错误.

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.

因此,我必须在最终的RFC2882消息中进行此破解.

So I had to do this hack in the final RFC2882 message.

inputTxt = Replace(inputTxt, "Reply-To:", "Reply-To: " & FromEmail)

现在可以使用.

*********可以使用MailMessage.ReplyToList.Add()代替来解决此问题.因此,这里的问题是Gmail API中需要ReplyTo地址(即使可能会假定ReplyTo应该默认为From电子邮件). **********

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

这篇关于GMail API电子邮件退回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆