Forum guidelines
Email format
Posted By ASPbee on 06 May 2011

    I have an automatic windows service to send out the emails as scheduled. But the email message i have been receiving is appearing in plain text instead of HTML format. Can someone throw me few tips or ideas where i am making a mistake.



Re: Email format
Posted By Ravishankar Singh on 06 May 2011  
You can match your code from the below:
Look into Bold section.
MailMessage mail = new MailMessage(); mail.To = ""; mail.From = ""; mail.Subject = "this is a test email."; mail.BodyFormat = MailFormat.Html; mail.Body = "this is my test email body.<br><b>this part is in bold</b>"; SmtpMail.SmtpServer = "localhost";  //your real server goes here SmtpMail.Send( mail );
Did you used the mailformat option? mail format used class system
Hope this will helpful for u. :)

Ravishankar Singh visit:
Re: Email format
Posted By Abhimanyu K Vatsa on 07 May 2011  
hey you have to build HTML mail by setting the IsBodyHTML to true. Here you go:

public void sendmail(string v3, string v2, string v1) //replace the parameter or delete if needed
        string smsg = "Thank you for posting Article on our site, your post is very important for us:<br><br>";
        smsg += "<br><b>Title of Post: </b><br>" + "<a href=" + v1 + ">" + v2 + "</a>";
        smsg += "<br><b>Description of Post: </b><br>" + v3;
        smsg += "<br><br><br><br>";
        smsg += "<b>Administrator</b>";
//in above lines I'm building HTML body for mailing, here you can use any standerd html tags

        MailMessage message = new MailMessage();
            string ToEmailIDToSendEmail = getEmailAddressOfUser(); //i'm just receiving TO email address from SQL Server DB by calling function
            message.To.Add(new MailAddress(ToEmailIDToSendEmail)); //here you can directly use the TO mail
            message.From = new MailAddress("use the same address i.e. ur username that will be used below");

            message.Subject = "your email subject will be here";
            message.Body = smsg;
            message.IsBodyHtml = true; //always set this to true for html based mail

            SmtpClient client = new SmtpClient();
            client.Port = 587; // I uses port 587 for gmail, replace this if you needed
            client.Host = ""; //replace the host, i will be using gmail
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential("senderemailid or say user name", "password-of-mail-id");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = nc;
            //do your catch job here;

Hope this helps.

Senior Software Developer | Microsoft MVP | Author | Speaker | Blogger at ITORIAN.COM
Re: Email format
Posted By ASPbee on 10 May 2011  
Thanks for all your responses. But unfortunately i am not sure its still not working. Can you please go through the code below and let me know where i am doing wrong or if it needs any further modification.

private bool MailSend(bool SMS,string EmailFrom, string EmailTo, string EmailSubject, string EmailBody,ref Exception parmExc)
 MailMessage msg=null;
msg = new MailMessage(EmailFrom, EmailTo, EmailSubject, EmailBody);
if (!SMS)
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost");
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
             msg.EmailBody = "http://localhost:3171/WebApplication1/frmResponse.aspx?TraxIterID="+ ds.Tables[0].Rows[i].ItemArray[3].ToString()"";
if (DebugMailSending)
StringBuilder sbMailSendDebug = new StringBuilder();
sbMailSendDebug.AppendFormat("EMail Sent => From={0};To={1};Subject={2};Body={3}", EmailFrom, EmailTo, EmailSubject, EmailBody);
parmExc = null;
return true;
 catch (Exception exc1)
parmExc = exc1;
return false;
  } // end MailSend


C# Corner Annual Conference 2016


Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.