简体   繁体   中英

The SMTP server requires a secure connection from MVC App on server

I am using below code to send email from my MVC 5 app and is sending emails while running locally from visual studio, but not sending mails when I try to send from the server (shared windows hosting)

 public ActionResult TestMail()
    {
        try
        {
            string email_from = "email_from@gmail.com";
            string email_to = "email_to@gmail.com";
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(email_from);
                mail.To.Add(email_to);

                mail.Subject = "Reference form Id:" ;
                mail.Body = "<h1>Test Body </h1>";
                mail.IsBodyHtml = true;
                Attachment data = new Attachment(
                               Server.MapPath("~/JsonData/privacystatement.pdf"),
                               System.Net.Mime.MediaTypeNames.Application.Octet);
                mail.Attachments.Add(data);

                using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential("email_from@gmail.com", "passcode");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
                ViewBag.Message = "Email sent.";
            }
        }
        catch (Exception ex)
        {
            ViewBag.Message = "Email not sent Error." + ex.Message;
        }

        return View("Finish");
    }

I am getting this error from server

    Email not sent Error.The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. 

My current account Settings:

    Less secure app access ON 
    Two factor Authentication (SMS on phone etc) DISABLED

While running locally this code is working and email is successfully sent but while upload code to server, then only i am getting this error

Should I add some more properties to ensure this will run on the server (shared windows hosting) as well

There are 2 aspects.

One is the code itself. I create a SmtpClient like that:

new SmtpClient
{
    Port = 587,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    EnableSsl = true,
    Host = "smtp.gmail.com",

    Credentials = new NetworkCredential(mailFrom, password)
};

Second is setting the Gmail account to allow such actions. You will have to set it up in that Gmail account. Please refer to this resource https://support.google.com/accounts/answer/6010255?hl=en

It contains a simple step-by-step instruction to do that.

After this, my code sends emails through Gmail from app hosted on standard windows hosting provider without problems.

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