在.Net中使用GMail API。使用Net.Mail.MailMessage创建消息传递。然后使用MimeKit创建MimeMessage(使用此方法发送附件+ HTML消息)。将MimeMessage.ToString传递给Base64编码器。没有API错误。代码运行正常。我可以在GMail的已发送页面中看到该消息。邮件看起来很完美(发送实际返回消息ID)。但是,在Gmail中,此邮件附加了以下附加邮件。
Bounce <nobody@gmail.com>
An error occurred. Your message was not sent.
像往常一样,Google没有其他信息。如何解决这个问题?
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是最令人沮丧的事情。
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"
]
}
答案 0 :(得分:4)
这很疯狂。这就是问题所在。
这一行
msg.ReplyTo = New MailAddress(FromEmail, SenderName)
出于任何原因(我猜当FromEmail和ReplyTo是相同的电子邮件时)将RFC2822 Reply-To
参数留空。即使注释msg.ReplyTo
,参数仍为空白。毋庸置疑,GMail API似乎存在回复问题留空的问题。绝对是一个编程错误。
所以我必须在最终的RFC2882消息中做这个黑客。
inputTxt = Replace(inputTxt, "Reply-To:", "Reply-To: " & FromEmail)
现在有效。
*********如下面的评论所指出,您可以使用MailMessage.ReplyToList.Add()
来解决此问题。因此,问题是Gmail API中需要ReplyTo
地址(尽管有人可能会认为ReplyTo
应默认为From
电子邮件)。 **********
答案 1 :(得分:2)
以防万一有人偶然发现并希望获得C#答案,以下是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("=", "");
}