0

Hi i am trying to send an email using gmail credentials and calling an email template but it is throwing an exception that Failure sending mail

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu@gmail.com", "xxxxxx");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
message = new MailMessage();
message.Subject = "Visitor Arrived";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = false;
message.Body = "EmailTemplate.html";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new MailAddress("ambarishkesavarapu@gmail.com");
message.To.Add(lblCPEmail.Text);
message.Priority = MailPriority.High;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpServer.Send(message);

Please help me out.

Hmxa Mughal
  • 394
  • 1
  • 4
  • 17
Ambarish
  • 497
  • 1
  • 5
  • 6

3 Answers3

0

You have to use StreamReader to read from html file....and also set the MailMessage.BodyFormat property to MailFormat.Html
Use This:

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu@gmail.com", "xxxxxx");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your 
{                                                         // HTML file
    message = new MailMessage();
    message.Subject = "Visitor Arrived";
    message.SubjectEncoding = System.Text.Encoding.UTF8;
    message.IsBodyHtml = false;
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.From = new MailAddress("ambarishkesavarapu@gmail.com");
    message.To.Add(lblCPEmail.Text);
    message.Priority = MailPriority.High;

    message.Body = reader.ReadToEnd();  // Load the content from your file...
    //...
}
SmtpServer.Send(message);

Answer partially taken from Send a email with a HTML file as body (C#)

Community
  • 1
  • 1
Hmxa Mughal
  • 394
  • 1
  • 4
  • 17
0
            foreach (GnrDataMailInfo dmi in lstDMI)
            {
                MailMessage mail = new MailMessage();
                mail.From = "youremail";

                SmtpClient smtp = new SmtpClient();
                smtp.Port = 25;   // [1]  port
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
                smtp.UseDefaultCredentials = false; // [3] Changed this
                smtp.Credentials = new NetworkCredential(mail.From.ToString(), "password");  // [4] Added this.
                smtp.EnableSsl = false;
                smtp.Timeout = 20000;
                smtp.Host = "yourhost";

                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.HeadersEncoding = System.Text.Encoding.UTF8;
                mail.Subject = dmi.Subject;
                mail.To.Add(dmi.To);
                mail.IsBodyHtml = true;
                mail.Body = dmi.Body;

                smtp.Send(mail);
            }
0

use port 465 instead

Port 465 is for smtps SSL encryption is started automatically before any SMTP level communication.

Port 587 is for msa It is almost like standard SMTP port. MSA should accept email after authentication (e.g. after SMTP AUTH). It helps to stop outgoing spam when netmasters of DUL ranges can block outgoing connections to SMTP port (port 25).

Panup Pong
  • 1,871
  • 2
  • 22
  • 44