-1

When I send mail to my gmail account, it shows below error.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required... code I am using is below

MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
     m.From = new MailAddress("me@gmail.com");
     m.To.Add("me@gmail.com");
     m.Subject = "This is a Test Mail";
     m.IsBodyHtml = true;
     m.Body = "test gmail";
     sc.Host = "smtp.gmail.com";
     sc.Port = 587;
     sc.Credentials = new System.Net.NetworkCredential("me@gmail.com", "passward");
     sc.UseDefaultCredentials = true;
     sc.EnableSsl = true;
     sc.Send(m);
     Response.Write("Email Send successfully");
}
catch (Exception ex)
{
     Response.Write(ex.Message);
}
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317

2 Answers2

1

Screen shot

Just tried your code, had to fiddle with a couple things but was sent this. Funny because I have done this previously using Gmail smtp (couple years back). But it looks like they are now verifying apps that use their platform.

Either use another smtp server that you are signed up to, or use your own. (there must be a test one that is available online??). Pretty sure sendgrid do a free trial.

Jamadan
  • 2,223
  • 2
  • 16
  • 25
0
using System.Net;
using System.Net.Mail;


string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "email@yahoo.com";
string password = "abcdefg";
string emailTo = "someone@domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    // Can set to false, if you are sending pure text.

    mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
    mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}

Please try this this should work for you Thank you

zafar hayat
  • 98
  • 1
  • 8