To Send an Email using SMTP Credentials you can use below code to send effectively.
For Email Providers such has GMAIL you need to “Allow Less Secure Apps” from your Account to send email without failure.
Below is a working and tested code in C#.
System.Net.Mail.MailMessage emailMsg = new MailMessage();
emailMsg.Subject = Subject;
emailMsg.Body = Body;
emailMsg.From = ConfigurationManager.AppSettings["FromEMail"];
emailMsg.To.Add(ToemailID);
emailMsg.Bcc.Add(bccemailID);
emailMsg.CC.Add(ccemailID);
emailMsg.IsBodyHtml = true;
emailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = ConfigurationManager.AppSettings["smtpHost"];
smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUserId"], ConfigurationManager.AppSettings["smtpPassword"]);
smtpClient.EnableSsl = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
try
{
smtpClient.Send(emailMsg);
Response = "Email sent";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Visits: 17155