public static string Send(string to, string subject, string content, string from = "")
{
try
{
MimeMessage message = new MimeMessage();
message.Subject = subject;
message.Body = new TextPart("Plain") { Text = content };
message.From.Add(new MailboxAddress(from));
message.To.Add(new MailboxAddress(to));
SmtpClient smtp = new SmtpClient();
smtp.Connect(
"smtp.live.com"
, 587
, MailKit.Security.SecureSocketOptions.StartTls
);
smtp.Authenticate("Username@hotmail.com", "Password");
smtp.Send(message);
smtp.Disconnect(true);
return "Success";
}
catch (Exception ex)
{
return $"Failed. Error: {ex.Message}";
}
}
使用gmail
smtp.Connect(
"smtp.gmail.com"
, 587
, MailKit.Security.SecureSocketOptions.StartTls
);
我尝试修改其他网站文章中的一些属性。
但是,我通常会收到以下错误消息:
"失败。错误:SMTP服务器不支持身份验证。"
"失败。错误:根据远程证书无效 验证程序。"
如何正确设置属性?
答案 0 :(得分:4)
我在ASP Core中使用MimeKit发送到Gmail。这是我项目中的一个片段,对我有用:
using (var client = new SmtpClient())
{
client.Connect(_appSettings.SmtpServerAddress, _appSettings.SmtpServerPort, SecureSocketOptions.StartTlsWhenAvailable);
client.AuthenticationMechanisms.Remove("XOAUTH2"); // Must be removed for Gmail SMTP
client.Authenticate(_appSettings.SmtpServerUser, _appSettings.SmtpServerPass);
client.Send(Email);
client.Disconnect(true);
}