Wednesday, January 28, 2009

How to send HTML content in Mail?

We can find many mails that we are in loop among friends like sending birthday wishes, festival, advertisements, captions, scenaries, etc…
I would like to share my code that could help to send such kind of mails very easily.
I have created a windows form which asks for to/Form mail ID, From Mail ID’s password, Mail subject, Body.

In this body is an html file which you can browse. So that the content of the browsed html file is considered as body. So, the mail is sent saying that its content is html.
I am using Google mail server which helps me to send mails globally.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.Mail;
namespace SendMail
public partial class frmSendMail : Form
string strTitle = "Send Mail";
public frmSendMail()
private void btnBrowse_Click(object sender, EventArgs e)
lblPath.Text = opnFldlg.FileName;

private void btnSend_Click(object sender, EventArgs e)
if (txtPassword.Text.Trim().Length == 0 || txtToMailID.Text.Trim().Length == 0 || txtFromMail.Text.Trim().Length == 0 || txtSubject.Text.Trim().Length == 0 || lblPath.Text.Trim().Length == 0)
throw new Exception("Please enter detals");
btnSend.Text = "Sending...";
MessageBox.Show(SendMail(txtFromMail.Text.Trim(), txtPassword.Text.Trim(), txtToMailID.Text.Trim(), File.ReadAllText(lblPath.Text.Trim()), txtSubject.Text.Trim()), strTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); }
catch (Exception exp)
MessageBox.Show(exp.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSend.Text = "Send";
private string SendMail(string strFromMailID, string strPswd,string strToMailID, string strBody, string strSubject)
SmtpMail.SmtpServer = "";
MailMessage message = new MailMessage();
message.Fields.Add("", "1");
message.Fields.Add("", strFromMailID);
message.Fields.Add("", strPswd);
message.Fields.Add("", "465");
message.Fields.Add("", "true");
message.From = strFromMailID;
message.To = strToMailID;
message.BodyFormat = MailFormat.Html;
message.Subject = strSubject;
message.Body = strBody;
return "Mail sent successfully to " + strToMailID;
catch (Exception exp)
throw exp;

Say suppose I would like to wish my friend for his birthday with a greeting card.
I found a greeting card through google at

Now I will prepare a html page that includes this image and few lines with my comments in it.
Now I can browse this html page as body through my windows forms and send it to my friend.
My HTML page using this image url

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Hi Friend......
<img src="" width='50%' /> </td></tr>

Html Page output :-

I saved this html file as “wishes.htm”.
Now I am filling the form with the details to send this html content as mail.

When I click on send,

Now I wish to open my 2 gmails (vsssrinivas85 and surya.srinivas85) to check this mail in outbox and in box respectively.

Vsssrinivas85 - Outbox

surya.srinivas85- Inbox

No comments:

Post a Comment