Sometime the email is sending on local machine but not on the server machine it gives the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required"
To resolve these we add the 2 lines extra for sending email using gmail
1.smtpClient.UseDefaultCredentials = false;
2.smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
Here below is the complete code for sending email using gmail
public static bool SendEmail(string emailAddress, string bodyMessage, string Subject)
{
smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
message = new MailMessage();
message.IsBodyHtml = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("Username", "pwd");
smtpClient.EnableSsl = true;
message.From = new MailAddress("emailfromaddress", "emailheader");
message.Body = bodyMessage;
message.Subject = Subject;
message.To.Add(emailAddress);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
}
0 Comment(s)