Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The SMTP server requires a secure connection or the client was not authenticated error while sending email from asp.net

Tags:

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 on the webserver, I got the error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required,

Any one can help? Thanks

like image 674
Hassan Mokdad Avatar asked Jun 25 '11 09:06

Hassan Mokdad


2 Answers

Finally I managed to solve it, The SSL should be enable on the webserver as well, here is a link to enable ssl on the IIS7

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

like image 91
Hassan Mokdad Avatar answered Sep 28 '22 16:09

Hassan Mokdad


Please try this

private void MailSendThruGmail()
    {
        MailAddress fromAddress = new MailAddress("[email protected]", "From Name");
        MailAddress toAddress = new MailAddress("[email protected]", "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());
        }
    }
like image 35
Sajith A.K. Avatar answered Sep 28 '22 16:09

Sajith A.K.