Sakshi Jain

Sakshi Jain

  • NA
  • 227
  • 19.6k

How to send Email in asp.net c# using console application

Dec 3 2015 6:56 AM
Hi,

I am trying to send bulk of emails through smtp server using gmail account, but i am facing issue like this: "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. ry1sm18393619pab.30 - gsmtp".
 
I don't understand why this issue is coming. I have given correct credentials like: Emailid and password for gmail, i have changed port number also 25,587. If anyone has some solution then it would be very helpful. Below is my code :
 
static void Main(string[] args)
{
EmailSend send = new EmailSend();
send.Page_Load();
}

protected void Page_Load()
{
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress("[email protected]"), new System.Net.Mail.MailAddress("[email protected]"));
msg.Subject = "Yavun reset password link..";
// msg.Body = string.Format("http://localhost:25430/api/resetpassword?Email" + EmailId);

// Can set to false, if you are sending pure text.
msg.IsBodyHtml = true;
string emailid = "[email protected]";
string password = "abc123";
string host = "smtp.gmail.com";

SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = host;
smtpclient.EnableSsl = true;
smtpclient.Port = 587;

//smtpclient.UseDefaultCredentials = true;
smtpclient.UseDefaultCredentials = true;
//smtpclient.Credentials = new System.Net.NetworkCredential(emailid, password);
smtpclient.Credentials = new System.Net.NetworkCredential(emailid, password);
smtpclient.Send(msg);
// return Ok("A link has been send to you for resetting password.. Check you mail!!");
Console.WriteLine("A link has been send to you for resetting password. Check your mail! ");
}
catch (Exception ex)
{
//Response.Write("Could not send the e-mail - error: " + ex.Message);
Console.WriteLine("Could not send the e-mail - error: " + ex.Message);
}
}
}
}
 
Can anyone tell me where i am going wrong?

Answers (4)