0

net code for sending mail via Gmail is not working did everything like less secure apps turned on, powerful password, with and without app password, with and without two-step verification.

coding:-

{
    var fromAddress = new MailAddress("doctor@gmail.com");    
    var fromPassword = "idntcare";        
    var toAddress = new MailAddress("thevarghese@gmail.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);    
}
SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19
Vargiz
  • 29
  • 8
  • What do you mean by "it's not working"? What is the problem ? What exception dou yo have? Also I see 2 problems in your code: You forget the `;` after the using code. You CANNOT use `message` outside the `using` block. – Rumpelstinsk May 23 '17 at 05:52
  • using(var message ...........) once this is executed gets disposed. So, smtp.Send(message) can not send disposed message. – Bhuban Shrestha May 23 '17 at 05:53
  • The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – Vargiz May 23 '17 at 05:59

3 Answers3

2

Try doing this

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

Or try this

using (MailMessage mm = new MailMessage(from, emailTo))
{
    var mailAddress = new MailAddress("address", "name for id");
    mm.From = mailAddress;
    mm.Subject = title;
    mm.Body = HttpUtility.HtmlDecode(body);
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential(from, password);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = NetworkCred;
    smtp.Send(mm);
}
Aditya Korti
  • 692
  • 2
  • 12
  • 28
  • am getting the same error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required – Vargiz May 23 '17 at 06:10
  • https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Aditya Korti May 23 '17 at 06:13
1

try this.

MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress("doctor@gmail.com");
// Recipient e-mail address.
Msg.To.Add("thevarghese@gmail.com");
Msg.Subject ="subject";
Msg.Body = "body";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential("user name","password");
smtp.EnableSsl = true;
smtp.Send(Msg);
SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19
  • am getting the same error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required – Vargiz May 23 '17 at 06:09
  • I didn't get why it's not working for you. follow this link. https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – SynozeN Technologies May 23 '17 at 06:12
1
string fromAddress ="doctor@gmail.com";    
string fromPassword = "idntcare";        
string toAddress = "thevarghese@gmail.com"; 
using (MailMessage mm = new MailMessage(fromAddress, toAddress))
    {
        mm.Subject = subject;
        mm.Body = body;

        //attachment
        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = true;

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;

        NetworkCredential NetworkCred = new NetworkCredential(email, password);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        //smtp.Port = 25;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }

Try These, you can use port number 587 or 25.

Hanni
  • 86
  • 5
  • am getting the same error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required – Vargiz May 23 '17 at 06:10
  • yes, you need to change some setting in gmail which u use to sent (fromAddress). – Hanni May 23 '17 at 06:13
  • 1
    check this link https://galleryserverpro.com/use-gmail-as-your-smtp-server-even-when-using-2-factor-authentication-2-step-verification/ – Hanni May 23 '17 at 06:23