Google 电子邮件 API 引发 400 错误。需要收件人地址 (c#)
Google Email API is raising 400 error. Recipient address required (c#)
我看过类似的 但我仍然不确定如何继续。
最近我升级了我的 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 网站上向我推荐的,他们提供了解释:
The problem with MimeMessage.ToString()
is that it's often impossible
to represent a MIME message entirely in 1 text encoding and so MimeKit
has no choice but to use ISO-8859-1 in order to be consistent.
Internally, what ToString()
does is message.WriteTo (memoryStream)
and
then convert the memory stream buffer into a string using the
ISO-8859-1 text encoding.
If you have any non-ASCII (and non-ISO-8859-1) characters in your
message, it may break things.
MimeKit
, up until 2.14 or so, used to add a warning header
"X-MimeKit-Warning: Do NOT use ToString()
! Use WriteTo()
instead!" to
the top of every message if you used ToString()
.
我看过类似的
最近我升级了我的 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 网站上向我推荐的,他们提供了解释:
The problem with
MimeMessage.ToString()
is that it's often impossible to represent a MIME message entirely in 1 text encoding and soMimeKit
has no choice but to use ISO-8859-1 in order to be consistent.Internally, what
ToString()
does ismessage.WriteTo (memoryStream)
and then convert the memory stream buffer into a string using the ISO-8859-1 text encoding.If you have any non-ASCII (and non-ISO-8859-1) characters in your message, it may break things.
MimeKit
, up until 2.14 or so, used to add a warning header "X-MimeKit-Warning: Do NOT useToString()
! UseWriteTo()
instead!" to the top of every message if you usedToString()
.