在 ubuntu return base64 错误上通过 .net 核心发送电子邮件
Sending email via .net core on ubuntu return base64 error
我在 ubuntu 16.04 上尝试通过 运行 的测试服务器发送电子邮件时遇到错误。我在 OVH 上有一个专业帐户,我正在使用这个 smtp:
pro1.mail.ovh.net
当我在 windows 10 上调试我的工作站 运行 时,我可以发送我的电子邮件。
这是我的代码:
var smtp = m_emailConfiguration["Smtp"];
var email = m_emailConfiguration["Email"];
var password = m_emailConfiguration["Password"];
try
{
using (var smtpClient = new SmtpClient(smtp, 587))
{
var mailMessage = new MailMessage(email, mailTo, subject, body);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(email, password);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message);
}
我遇到了这个错误:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters
完整堆栈跟踪:
Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64String(String s)
at System.Net.Mail.SmtpNegotiateAuthenticationModule.GetSecurityLayerOutgoingBlob(String challenge, NTAuthentication clientContext)
at System.Net.Mail.SmtpNegotiateAuthenticationModule.Authenticate(String challenge, NetworkCredential credential, Object sessionCookie, String spn, ChannelBinding channelBindingToken)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
我发现有人遇到了同样的问题:
但是他的问题是密码,我确定我的密码是正确的。
那么有人有想法吗?
谢谢
让我们把所有的评论都放在一个答案中。
不要使用 SmptClient。使用 MailKit instead. SmtpClient is obsolete and Microsoft itself recommends using MailKit 或其他库。
调用堆栈显示在尝试使用 Windows 身份验证进行身份验证时出现错误。在这种情况下,这显然是错误的,但错误可能不会准确修复,因为 class 已过时。
Sending Messages 示例展示了发送消息是多么容易:
using (var client = new SmtpClient ()) {
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s,c,h,e) => true;
client.Connect ("smtp.friends.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
}
MailKit 建立在 MimeKit 之上,这意味着它可以轻松创建复杂的消息。从 another example 复制,您可以使用 BodyBuilder
实用程序 class 创建包含纯文本正文和带图像的 HTML 正文的消息。
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";
var builder = new BodyBuilder ();
// Set the plain-text version of the message text
builder.TextBody = @"Hey Alice,
....
-- Joey
";
// In order to reference selfie.jpg from the html text, we'll need to add it
// to builder.LinkedResources and then use its Content-Id value in the img src.
var image = builder.LinkedResources.Add (@"C:\Users\Joey\Documents\Selfies\selfie.jpg");
image.ContentId = MimeUtils.GenerateMessageId ();
// Set the html version of the message text
builder.HtmlBody = string.Format (@"<p>Hey Alice,<br>
....
<p>-- Joey<br>
<center><img src=""cid:{0}""></center>", image.ContentId);
// We may also want to attach a calendar event for Monica's party...
builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");
// Now we just need to set the message body and we're done
message.Body = builder.ToMessageBody ();
正如 Panagiotis Kanavos 在评论中所说,SmtpClient 已被弃用。我改用 MailKit,它现在工作正常。
我在 ubuntu 16.04 上尝试通过 运行 的测试服务器发送电子邮件时遇到错误。我在 OVH 上有一个专业帐户,我正在使用这个 smtp:
pro1.mail.ovh.net
当我在 windows 10 上调试我的工作站 运行 时,我可以发送我的电子邮件。
这是我的代码:
var smtp = m_emailConfiguration["Smtp"];
var email = m_emailConfiguration["Email"];
var password = m_emailConfiguration["Password"];
try
{
using (var smtpClient = new SmtpClient(smtp, 587))
{
var mailMessage = new MailMessage(email, mailTo, subject, body);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(email, password);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message);
}
我遇到了这个错误:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters
完整堆栈跟踪:
Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64String(String s)
at System.Net.Mail.SmtpNegotiateAuthenticationModule.GetSecurityLayerOutgoingBlob(String challenge, NTAuthentication clientContext)
at System.Net.Mail.SmtpNegotiateAuthenticationModule.Authenticate(String challenge, NetworkCredential credential, Object sessionCookie, String spn, ChannelBinding channelBindingToken)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
我发现有人遇到了同样的问题:
但是他的问题是密码,我确定我的密码是正确的。
那么有人有想法吗?
谢谢
让我们把所有的评论都放在一个答案中。
不要使用 SmptClient。使用 MailKit instead. SmtpClient is obsolete and Microsoft itself recommends using MailKit 或其他库。
调用堆栈显示在尝试使用 Windows 身份验证进行身份验证时出现错误。在这种情况下,这显然是错误的,但错误可能不会准确修复,因为 class 已过时。
Sending Messages 示例展示了发送消息是多么容易:
using (var client = new SmtpClient ()) {
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s,c,h,e) => true;
client.Connect ("smtp.friends.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
}
MailKit 建立在 MimeKit 之上,这意味着它可以轻松创建复杂的消息。从 another example 复制,您可以使用 BodyBuilder
实用程序 class 创建包含纯文本正文和带图像的 HTML 正文的消息。
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";
var builder = new BodyBuilder ();
// Set the plain-text version of the message text
builder.TextBody = @"Hey Alice,
....
-- Joey
";
// In order to reference selfie.jpg from the html text, we'll need to add it
// to builder.LinkedResources and then use its Content-Id value in the img src.
var image = builder.LinkedResources.Add (@"C:\Users\Joey\Documents\Selfies\selfie.jpg");
image.ContentId = MimeUtils.GenerateMessageId ();
// Set the html version of the message text
builder.HtmlBody = string.Format (@"<p>Hey Alice,<br>
....
<p>-- Joey<br>
<center><img src=""cid:{0}""></center>", image.ContentId);
// We may also want to attach a calendar event for Monica's party...
builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");
// Now we just need to set the message body and we're done
message.Body = builder.ToMessageBody ();
正如 Panagiotis Kanavos 在评论中所说,SmtpClient 已被弃用。我改用 MailKit,它现在工作正常。