2016-12-27 163 views
0

我正在使用ASP.Net Core并尝试使用Gmail中的smtp客户端发送电子邮件。有下面的代码,但它不工作 看过下面的帖子,以及,但它不工作 http://dotnetthoughts.net/how-to-send-emails-from-aspnet-core/通过gmail使用SMTP发送电子邮件

它thorws以下错误

System.NotSupportedException:SMTP服务器不支持身份验证

var emailMessage = new MimeMessage(); 
emailMessage.From.Add(new MailboxAddress("From Name", "[email protected]")); 
emailMessage.To.Add(new MailboxAddress("To Name", "[email protected]")); 
emailMessage.Subject = subject; 

var bodyBuilder = new BodyBuilder(); 
bodyBuilder.HtmlBody = message; 
emailMessage.Body = bodyBuilder.ToMessageBody(); 

var client = new SmtpClient(); 
try 
{ 
    await client.ConnectAsync("smtp.gmail.com", 25, SecureSocketOptions.None).ConfigureAwait(false); 
    client.AuthenticationMechanisms.Remove("XOAUTH2"); 
    await client.AuthenticateAsync("[email protected]", "fromPassword"); //error occurs here 

    await client.SendAsync(emailMessage).ConfigureAwait(false); 
    await client.DisconnectAsync(true); 
    await client.DisconnectAsync(true).ConfigureAwait(false); 
} 
catch(Exception e) 
{ 

} 
+3

为什么在示例显示端口587时使用端口25? – mason

+0

@mason点是正确的。我们可以使用587或465端口号在Gmail中使用smtp !.我不知道你为什么使用25。 – RajeeshMenoth

回答

0

NotSupportedException被抛出,因为Gmail没有不使用SSL/TLS连接,因为它支持认证仅支持非加密的基于密码的身份验证机制。

我会建议连这样的:

client.ConnectAsync("smtp.gmail.com", 587, SecureSocketOptions.StartTls) 

希望有所帮助。

相关问题