简体   繁体   中英

Issue in SMTP email sending, ASP .Net mvc application hosted in Azure

I am new to azure. My asp .net MVC application hosted in azure. This application has email sending functionality. When the application is moved to azure, email functionality is not working. My error logs displays error as below:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. a65sm9218660oih.6 - gsmtp
   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

Code block used to send emails

SmtpClient client = new SmtpClient(GetStringValue(EMAIL_CLIENT), GetIntValue(EMAIL_PORT))
            {
                Credentials = new NetworkCredential(GetStringValue(EMAIL_USER_NAME),                            GetStringValue(EMAIL_PASSWORD)),
                EnableSsl = sslOn
            };

client.Host = SettingsManager.GetStringValue("EmailClient");//smtp.gmail.com
client.Port = SettingsManager.GetIntValue("EmailPort");//587
client.DeliveryMethod = SmtpDeliveryMethod.Network;

Can anyone please help me to resolve this problem. Thank you.

Even I faced the same issue with gmail. Gmail block your emails from azure, because your app is trying to log in from a location with different timezone or different from the one you used to create the account. Check your gmail inbox and it will have an email regarding blocked login attempt.

The solution is to either login to gmail from your azure server or check the blocked email in your inbox and add that device to verfied devices. ie select "I recognize this activity as mine" as mentioned in this link .

This is fully working code from an app we had at one time hosted on Azure. The email sent fine through gmail

MailMessage mail = new MailMessage();
mail.From = new MailAddress("Support@domain.com");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(email));
mail.Body = "Email body";

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("gmail-email-address", "gmail-password");
smtp.EnableSsl = true;
smtp.Send(mail);

First check your code, there is issues on port, credentials, timeout, ...

var smtp = new SmtpClient
{               
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, "yourApplicationSpecificPassword"),
    Timeout = 10000
};
using (var message = new MailMessage(fromAddress, toAddress)
{                
    IsBodyHtml = true,               
    Subject = subject,
    Body = body

})
{
    smtp.Send(message);
}

Then in gmail setting, use 2 step verification - https://myaccount.google.com/u/3/signinoptions/two-step-verification

and generate app passwd - https://myaccount.google.com/u/3/apppasswords 在此处输入图片说明

use generated passwd in code "yourApplicationSpecificPassword"

good luck!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM