Kasuni Abegunawardana

Kasuni Abegunawardana

  • NA
  • 211
  • 125.5k

Send Mail is not working asp.net web forms

Dec 26 2017 12:02 PM
Hi,
 
I got an error like this.
 
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
 
Source Error:
tsmtpClient.Send(tmailmessage);
 
private static void SenEmail(string from, string to, string cc, string bcc, string subject, string body, bool IsHTML)
{
//create an instant in mail message
MailMessage tmailmessage = new MailMessage();
//assign sender mail address
tmailmessage.From = new MailAddress(from);
//asssign recipiant email address
tmailmessage.To.Add(new MailAddress(to));
//check if cc/bcc is not null
if (cc != null && cc != "")
tmailmessage.CC.Add(new MailAddress(cc));
if (bcc != null && bcc != "")
tmailmessage.Bcc.Add(new MailAddress(bcc));
//assign the subject
tmailmessage.Subject = subject;
//assign the mail body
tmailmessage.Body = body;
//assign the format into mail box
tmailmessage.IsBodyHtml = IsHTML;
//assign the prority of the mail into normal
tmailmessage.Priority = MailPriority.Normal;
//subject encoding by UTF-8
tmailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
//body encoding by UTF-8
tmailmessage.BodyEncoding = System.Text.Encoding.UTF8;
//Create a new instance of SMTP client and pass name port number from SMTP gmail server
SmtpClient tsmtpClient = new SmtpClient("smtp.gmail.com", 587);
//enable SSL of theamtp client
tsmtpClient.EnableSsl = true;
//use delivery method as network
tsmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//use the Credentials set to false
tsmtpClient.UseDefaultCredentials = false;
//pass account information to the sender
tsmtpClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
tsmtpClient.Send(tmailmessage);
}

Answers (5)