0

I have made a mailsender in C#.but the program does a error when I run it.

this is my code:

MailMessage mail=new MailMessage("shahidi83am@gmail.com","shahidi83am@yahoo.com","subject","this is a test");
        SmtpClient smtp =new SmtpClient("smtp.gmail.com");
        smtp.Port=587;
        smtp.Credentials= new System.Net.NetworkCredential("username","pass");
        smtp.Send(mail);

and this is the error:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 79sm6578238wrc.34 - gsmtp

  • 1
    Try setting [`.EnableSsl=true;`](https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx) – freefaller Jul 02 '17 at 10:15

2 Answers2

0

I use gmail with these parameters;

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(smtp_user, smtp_pass);
smtpClient.Send(message);
Samil
  • 39
  • 5
-1

Is username and pass in the line below are raw string? then error will occur.

smtp.Credentials= new System.Net.NetworkCredential("username","pass");

In networkCredential you have to provide sender's email and password.

for example:

string username = "abc@example.com";
string pass = "whatever_your_password_is";
smtp.Credentials= new System.Net.NetworkCredential(username,pass);