Send Email in ASP.NET 2.0


========================================
Method 1
========================================
This is a simple article which tells you how you can embed images in emails.This can be very useful when you want to send emails from your website.A common scenario would be when a new user registers with your site and you want to send a welcome email with your site’s logo on top.

I searched the net when I faced this issue. I am grateful to many authors from where I could make a  start. I have used their code while writing this.Due credit goes to them all. The intention is to make the code available for the others too.  To use this all you have to so is copy the code and paste it in any button click event.

This may only work with ASP.NET 2.0 & C#. I have tested it too.It will not run in ASP.NET 1.1.

Namespaces Used

using System.Net.Mail;
using System.Net.Mime;

CODE

// send mail to the new user who has registered.
protected void yourButton_Click(object sender, EventArgs e)
{

string strMailContent = “Welcome new user”;
string fromAddress = “yourname@yoursite.com”;
string toAddress = “newuser@hisdomain.com”;
string contentId  = “image1”;

// my logo is placed in images folder
string path = Server.MapPath(@”images/Logo.jpg”);

MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
mailMessage.Bcc.Add(“inkrajesh@hotmail.com”); // put your id here
mailMessage.Subject = “Welcome new User”;

LinkedResource logo = new LinkedResource(path);
logo.ContentId = “companylogo”;

// done HTML formatting in the next line to display my logo

AlternateView av1 = AlternateView.CreateAlternateViewFromString
(“<html><body><img src=cid:companylogo/><br></body></html>” +
strMailContent, null, MediaTypeNames.Text.Html);

av1.LinkedResources.Add(logo);

mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;

//use this if you are in the development server

SmtpClient mailSender = new SmtpClient(“localhost”);

mailSender.Send(mailMessage);

}

========================================
Method 2 :
========================================
To show an image inside the body of the email without link to external site is necessary to add the attachment in the header of message and call it from the HTML section of the body. Using the MailMessage and the SmtpClient classes is not enough set the Inline property to True to show the image:

// creating the attachment
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@”c:\\test.png”);
inline.ContentDisposition.Inline = true;

// sending the message
MailMessage email = new MailMessage();
// set the information of the message (subject, body ecc…)

// send the message
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(“localhost”);
smtp.Send(email);
email.Dispose();

In this way, the sent message misses the right Content-Type section in the header (to have an Inline attachment is necessary the “multipart/related” Content-Type).To resolve this situation is possible to by-pass the SmtpClient and use one custom class for SMTP client. This class provides to connect/communicate with the SMTP server and to add the right Content-Type to the message.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

namespace devSmtp {

class Program {

static void Main(string[] args)
{

// This example show you how to send one email message with an INLINE attachment.
// You can use this example also without the support of CDO or other type of SmtpClient.

// creating the email message
MailMessage email = new MailMessage(“test@yourdomain.something”, “test@yourdomain.something”);

// information
email.Subject = “INLINE attachment TEST”;
email.IsBodyHtml = true;
email.Body = “<div style=\”font-family:Arial\”>This is an INLINE attachment:<br /><br /><img

src=\”@@IMAGE@@\” alt=\”\”><br /><br />Thanks for downloading this example.</div>”;

// generate the contentID string using the datetime

string contentID = Path.GetFileName(attachmentPath).Replace(“.”, “”) + “@zofm”;

// create the INLINE attachment

string attachmentPath = Environment.CurrentDirectory + @”\test.png”;
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = “image/png”;
inline.ContentType.Name = Path.GetFileName(attachmentPath);
email.Attachments.Add(inline);

// replace the tag with the correct content ID

email.Body = email.Body.Replace(“@@IMAGE@@”, “cid:” + contentID);

// sending the email with the SmtpDirect class
//(not using the System.Net.Mail.SmtpClient class)

SmtpDirect smtp = new SmtpDirect(“localhost”);
smtp.Send(email);

email.Dispose();
}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s