UserManager.SendEmailAsync works on localhost but does not work on server

276 Views Asked by At

I am trying to send email to the user in asp.net MVC using identity 2. It works fine on localhost but when I upload it on server, it does not send email.

This is what I have in controller:

await UserManager.SendEmailAsync(user.Id, "EmailSubject", "emailText");

And this is what I have in IdentityConfig.cs:

public Task SendAsync(IdentityMessage message)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("myGmailAddress", "MyWebsiteAddress");
            mail.To.Add(message.Destination);
            mail.Subject = message.Subject;
            mail.Body = message.Body;
            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myGmailAddress", "myGmailPassword");
            SmtpServer.EnableSsl = true;            
            return SmtpServer.SendMailAsync(mail);
        }
1

There are 1 best solutions below

0
Niloufar Mon On

Well I changed the Smtp port to 25 like this:

SmtpServer.Port = 25;

And it worked!