Gmail Error :The SMTP server requires a secure con

2018-12-31 04:44发布

I am using following code to send email. The Code works correctly in my local Machine. But on Production server i am getting the error message

        var fromAddress = new MailAddress("mymailid@gmail.com");
        var fromPassword = "xxxxxx";
        var toAddress = new MailAddress("yourmailid@yourdoamain.com");

        string subject = "subject";
        string body = "body";

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)

        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })


        smtp.Send(message);

And on my Gmail A/c I have received the following email after i ran the code from production server

Hi ,

Someone recently used your password to try to sign in to your Google Account mymailid@gmail.com. This person was using an application such as an email, client or mobile device.

We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:

Friday, 3 January 2014 13:56:08 o'clock UTC IP Address: xxx.xx.xx.xxx (abcd.net.) Location: Philadelphia PA, Philadelphia, PA, USA

If you do not recognise this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.

Reset password

If this was you and you are having trouble accessing your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login

Yours sincerely, The Google Accounts team

标签: c# .net smtp gmail
23条回答
千与千寻千般痛.
2楼-- · 2018-12-31 05:11

If your are using gmail.

  • 1-logon to your account

    2- browse this link

    3- Allow less secure apps: ON

Enjoy....

查看更多
泛滥B
3楼-- · 2018-12-31 05:11

dont put break-point before await smtp.SendMailAsync(mail);

:)

when it waits with a break point giving this error when i remove break point it has worked

查看更多
时光乱了年华
4楼-- · 2018-12-31 05:12

Just follow the step in the google email and enable less secure apps.

查看更多
孤独总比滥情好
5楼-- · 2018-12-31 05:12

You should disallow less secure apps to access your google account.

to do:

https://myaccount.google.com/lesssecureapps

查看更多
像晚风撩人
6楼-- · 2018-12-31 05:13

NOVEMBER 2018, have tried everything above with no success.

Below is the solution that worked finally. Unfortunately it's not using SSL, but it works!!!

var fromAddress = new MailAddress(asd@asd.com, "From Name");
var toAddress = new MailAddress("tosend@asd.com", "To Name");

const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "aspmx.l.google.com",
    Port = 25,
    EnableSsl = false
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}
查看更多
浮光初槿花落
7楼-- · 2018-12-31 05:14

I had the same problem for an application deployed to Microsoft Azure.

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

First I approved all unknown devices (some ip-addresses originating from Ireland) on the following page (signed in as the gmail user): https://security.google.com/settings/u/1/security/secureaccount

I used the following settings for the client:

var client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my_user_name@gmail.com", "my_password"); 

It started working only after I set the following property on the smtp-client:

client.TargetName = "STARTTLS/smtp.gmail.com";
查看更多
登录 后发表回答