我见过这个类似的问题,但我仍然不确定如何进行。
最近我升级了我的Gmail API库,所以我不知道它是否相关。
我得到一个类似的错误:
<LogEntry Date="2021-08-22 12:35:10" Severity="Exception" Source="MSAToolsGMailLibrary.MSAToolsGMailLibraryClass.SendEmail" ThreadId="1">
<Exception Type="Google.GoogleApiException" Source="Google.Apis.Requests.ClientServiceRequest`1+<ParseResponse>d__35.MoveNext">
<Message>Google.Apis.Requests.RequestError
Recipient address required [400]
Errors [
Message[Recipient address required] Location[ - ] Reason[invalidArgument] Domain[global]
]
</Message>
<StackTrace> at Google.Apis.Requests.ClientServiceRequest`1.<ParseResponse>d__35.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
at MSAToolsGMailLibrary.MSAToolsGMailLibraryClass.SendEmail(String From, String Subject, String Attachment)</StackTrace>
</Exception>
</LogEntry>
即使我尝试我的测试代码,也会发生这种情况:
using Message = Google.Apis.Gmail.v1.Data.Message
public bool SendTestEmail(string From, string Subject, string Body)
{
try
{
MailMessage mail = new MailMessage();
mail.Subject = Subject;
mail.Body = Body;
mail.From = new MailAddress(From);
mail.IsBodyHtml = false;
mail.To.Add(new MailAddress(From));
MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mail);
Message message = new Message();
message.Raw = Base64UrlEncode(mimeMessage.ToString());
var result = m_Service.Users.Messages.Send(message, "me").Execute();
}
catch (Exception ex)
{
SimpleLog.Log(ex);
return false;
}
return true;
}
private string Base64UrlEncode(string input)
{
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(inputBytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
我确认我用正确的凭据连接到我的帐户。
这是发送电子邮件的正确方式:
public bool SendTestEmail(string From, string Subject, string Body)
{
try
{
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(MailboxAddress.Parse(From));
mimeMessage.ReplyTo.Add(MailboxAddress.Parse(From));
mimeMessage.To.Add(MailboxAddress.Parse(From));
mimeMessage.Subject = Subject;
mimeMessage.Body = new TextPart("plain")
{
Text = Body
};
Message message = new Message();
using (var memory = new MemoryStream())
{
mimeMessage.WriteTo(memory);
var buffer = memory.GetBuffer();
int length = (int)memory.Length;
message.Raw = Convert.ToBase64String(buffer, 0, length);
}
var result = m_Service.Users.Messages.Send(message, "me").Execute();
}
catch (Exception ex)
{
SimpleLog.Log(ex);
return false;
}
return true;
}
代码是由MimeKit
作者在他们的GitHub网站上好心地建议给我的,他们提供了一个解释:
MimeMessage.ToString()
的问题是它通常是不可能的来表示一个MIME消息完全在一个文本编码,所以MimeKit
为了保持一致,我们别无选择,只能使用ISO-8859-1。在内部,
ToString()
所做的是message.WriteTo (memoryStream)
和方法将内存流缓冲区转换为字符串ISO-8859-1文本编码。如果您的文件中有任何非ascii(和非iso -8859-1)字符消息,它可能会破坏东西。
MimeKit
,直到2.14左右,用于添加警告头" x - mimekit -警告:不要使用ToString()
!请使用WriteTo()
代替!">如果您使用ToString()
,则在每条消息的顶部。
这是我最终制作的发送电子邮件:
public static Message? SendEmail(
GmailService service,
string subject,
string body,
string recipientAddress,
string recipientName,
GmailOptions emailOptions
)
{
Message? responseMessage;
string senderAddress = emailOptions.AccountEmail;
string senderName = emailOptions.AccountDisplayName;
string raw;
using (var mimeMessage = new MimeMessage
{
Body = new TextPart("html") { Text = body },
Subject = subject
}) {
mimeMessage.To.Add(new MailboxAddress(recipientName, recipientAddress));
mimeMessage.From.Add(new MailboxAddress(senderName, senderAddress));
using (var stream = new MemoryStream())
{
mimeMessage.WriteTo(stream);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
string rawString = reader.ReadToEnd();
byte[] rawBytes = Encoding.UTF8.GetBytes(rawString);
raw = Convert.ToBase64String(rawBytes);
}
}
}
var messageBody = new Message() { Raw = raw };
var sendRequest = service.Users.Messages.Send(messageBody, userId: senderAddress);
responseMessage = sendRequest.Execute();
return responseMessage;
}