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.
Code:-

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()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
opnFldlg.ShowDialog();
lblPath.Text = opnFldlg.FileName;
}

private void btnSend_Click(object sender, EventArgs e)
{
try
{
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);
}
finally
{
btnSend.Text = "Send";
}
}
private string SendMail(string strFromMailID, string strPswd,string strToMailID, string strBody, string strSubject)
{
try
{
SmtpMail.SmtpServer = "smtp.gmail.com";
MailMessage message = new MailMessage();
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strFromMailID);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPswd);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
message.From = strFromMailID;
message.To = strToMailID;
message.BodyFormat = MailFormat.Html;
message.Subject = strSubject;
message.Body = strBody;
SmtpMail.Send(message);
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 http://www.4to40.com/images/egreetings/Birthday/Happy_Birthday_Wishes-Birthday-148_big.gif.




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">
<html>
<head>
<title></title>
</head>
<body>
<table><tr><td>
Hi Friend......
</td></tr>
<tr><td>
<img src="http://www.4to40.com/images/egreetings/Birthday/Happy_Birthday_Wishes-Birthday-148_big.gif" width='50%' /> </td></tr>
<tr><td>
Cheers,
</td></tr>
<tr><td>
Srinivas
</td></tr></table>
</body>
</html>

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: