The SMTP server requires a secure connection, or the client did not authenticate when sending email from asp.net

I am trying to send an email from my asp.net application, the function worked fine on my machine, but when I deployed it to a web server, I got an error: the SMTP server requires a secure connection or the client is not authenticated. Server response: 5.5.1 Authentication required

Anyone can help? Thanks

+5
source share
4 answers

I finally managed to solve it. SSL must also be enabled on the web server, here is the link to enable ssl on IIS7

http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx

+8

,

private void MailSendThruGmail()
    {
        MailAddress fromAddress = new MailAddress("sender@gmail.com", "From Name");
        MailAddress toAddress = new MailAddress("recipient@gmail.com", "To Name");
        const string subject = "test";
        const string body = @"Using this feature, you can send an e-mail message from an application very easily.";

        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(fromAddress.Address, toAddress.Address, subject, body);
        msg.IsBodyHtml = true;

        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl = true
        };

        try
        {
            client.Send(msg);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
+6

. gmail, gmail .

+3

You are part of a domain, and the SMTP server is also located in this domain. If so, you may be automatically authenticated with Windows Authentication, and the user that IIS is working with is probably local to the machine on which it is enabled. You can either run the application pool as a domain user, or use impersonation (not verified, but should work) or programmatically add credentials to the SMTP server when sending email.

0
source

All Articles