Część probuję w asp.net mvc zrobić potwierdzenie maila po zarejestrowaniu sie na stronie. Niestety ciągle dostaję poniższy komunikat:
System.Net.Mail.SmtpException: 'Serwer SMTP wymaga bezpiecznego połączenia lub nie uwierzytelniono klienta. Response server: 5.5.1 Authentication Required. Learn more at'
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
//return Task.FromResult(0);
return Task.Factory.StartNew(() =>
{
sendMail(message);
});
}
void sendMail(IdentityMessage message)
{
#region formatter
string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
string html = "Please confirm your account by clicking this link: <a href=\"" + message.Body + "\">link</a><br/>";
html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);
#endregion
MailMessage msg = new MailMessage();
msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
msg.To.Add(new MailAddress(message.Destination));
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
//smtpClient.UseDefaultCredentials = true;
//smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
}
}
Plik Web.config:
<appSettings>
<add key="Email" value="test@gmail.com"/>
<add key="Password" value=""/>
</appSettings>
Próbowałem każda solucję z poniższego linku zrobić, niestety w dalszym ciągu nie działa.
https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not
Czy ktoś ma jakiś pomysł, jak mogę rozwiązać powyższy problem?