Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to send a mail which has HTML tags as mail body and this is the function i have written .
C#
private void SendMail(string EmailIDs, string EmailSubject, string EmailBody)
       {
           string strSMTPServer = "Server Address";
           string strSMTPFrom = "Sender Address";

           MailMessage mailMessage = new MailMessage(strSMTPFrom, EmailIDs);
           mailMessage.Subject = EmailSubject;
           mailMessage.Body = EmailBody;
           mailMessage.IsBodyHtml = true;
           SmtpClient smtpClient = new SmtpClient(strSMTPServer);
           smtpClient.Send(mailMessage);
       }


This is not working as the body that contains HTML tags are shown as tags only.

So i tried like this also


C#
private void SendMail(string EmailIDs, string EmailSubject, string EmailBody)
       {
           string strSMTPServer = "Server Address";
           string strSMTPFrom = "Sender Address";
           MailMessage mailMessage = new MailMessage(strSMTPFrom, EmailIDs);
           mailMessage.Subject = EmailSubject;
           mailMessage.Body = EmailBody;
           mailMessage.IsBodyHtml = true;
           AlternateView av = AlternateView.CreateAlternateViewFromString(EmailBody,      null, MediaTypeNames.Text.Html);
           mailMessage.AlternateViews.Add(av);

           SmtpClient smtpClient = new SmtpClient(strSMTPServer);
           smtpClient.Send(mailMessage);
       }


And this too is not working.

Does the html support depends on anyother factors? Can anyone please help me out
Posted
Comments
Mathi Mani 22-May-15 1:49am    
Can you post a sample message body? It will help identify the issue.
Sergey Alexandrovich Kryukov 22-May-15 2:41am    
I have an impression that you send correct mail (if HTML body is correct), but your client is not showing it.
Or, by some reason, you put the body with escaped HTML markup.
—SA
Arjun Menon U.K 22-May-15 3:42am    
Am really really sorry Mani, i found a > missing in my html ... Let me correct and check ... Thanks for the response
Arjun Menon U.K 22-May-15 3:44am    
Sergey, like replace < with < , > with > like that no?

1 solution

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(NOTIFICATION_MAIL, MAIL_PASSWORD);
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;
SmtpServer.Host = "smtp.gmail.com";
mail.From = new MailAddress(NOTIFICATION_MAIL);
mail.To.Add(ToADDRESS_MAIL);
mail.Subject = mailSubject;
mail.Body = messageBody;
SmtpServer.Send(mail);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900