Ø
Sending a Simple Mail
Ø
Sending Mail with Attachment
Ø
Sending Mail with HTML body
Ø
Sending Email with Embedded Image in the Message Body
Ø
Sending Email with Gmail SMTP Server from ASP.Net
Sending a Simple Mail
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);
The above code can be used to send a simple email.
Sending Mail with Attachment
We can also send email with attachments. The below code
will help you to achieve it.
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("From@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
mail.Attachments.Add(new Attachment(@"F:\Articles\Email in
ASP.Net 2.0\SendEmail\mail.png"));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);
Sending Mail with HTML Body
Sometimes, we will have requirements to send email as a
HTML body. The below code will help you to achieve the same.
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("From@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);
We may also have requirements to send email with an
embedded image in the message body. This was made easy with ASP.Net 2.0 with the
help 2 new classes that are packed in .Netframework 2.0. Next section will help
us understand the same.
Sending Email with Embedded Image in the Message
Body
We can embed image in the email body using
LinkedResource class and AlternateView class that is packed with System.Net.Mail
namespace and by making the message body as HTML.
How to achieve this?
We have to include a <IMG> with a contentid
specified in src attribute of <IMG> tag in the message body, i.e. the HTML
body should contain <img src="cid:imageId" />.
Example will be,
<b>View my Pic</b><br>
<img src="cid:Anyid" />
We need to give the same contentid to the ContentId
attribute of LinkedResource object for the code to work.
Implementation
try
{
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new
MailAddress("from@gmail.com");
mail.Subject = "Test with Image";
string Body = <b>Welcome to
codedigest.com!!</b><br><BR>Online resource for .net
articles.<BR><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline
border=0 >";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink = new
LinkedResource(Server.MapPath(".") + @"\codedigest.png", "image/png");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding =
System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod =
SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
|