1. Using MailMessage object and Creat email.
2. Send email Using SmtpClient object.
step 01 :
add System.Net.Mail namespace and System.Net Namespace to your project.
step 02 :
you need to Create MailMessage Object use following code.
MailMessage mail = new MailMessage();
//MailMessage constructor has 4 overload method
//1.string From/To
//MailMessage mail = new MailMessage("mgdhsandaruwan@yahoo.com","mgdhsandaruwan@gmail.com");
//2 MallAddress from/to
//3 string from/to/subject/body
//MailMessage mail = new MailMessage("mgdhsandaruwan@yahoo.com","mgdhsandaruwan@gmail.com","subject","body Details");
//4 no parameter
mail.From = new MailAddress("mgdhsandaruwan@yahoo.com", "Admin");
mail.To.Add(new MailAddress("mgdhsandaruwan@gmail.com","dinuka"));
// same way can be add many more address to mailaddress collection
//mail.Bcc.Add(); // same way to add bcc and cc
//mail.cc.add();
mail.Subject = " Mail subject";
mail.Body = "Mail body Content";
// mail.Attachments.Add(new Attachment("C:\windows..."));
if (chkAttach.Enabled)
{
Stream st = new FileStream(txtAttachFile.Text, FileMode.Open);
mail.Attachments.Add(new Attachment(st, "myFile.txt", MediaTypeNames.Application.Octet));
}
// see more details about MIME
follow these link
Create Html emails Massage
differences is that mail massage body is contain html tag .
ex :-
// Specify an HTML message body
mail.Body =
"My Message"This is an HTML message.";
mail.IsBodyHtml = true;
mail.header is also plain text. also email client ignore head section and client side scripting and do not download the image automatically web site.
There is special way to add image to the mail massage body without user downloaded image.
Embedded image to Mail massage body
First create html massage Using AlternateView and then add images using LinkedResource.
example :
mail.IsBodyHtml = true;
string bdy = "<"htmal"><"body">
" html body content "
<"br"><"img src"=\"cid:Pic1\">"
AlternateView alv = AlternateView.CreateAlternateViewFromString(bdy, null, MediaTypeNames.Text.Html);
LinkedResource lnkR = new LinkedResource("dinuka.gif", MediaTypeNames.Image.Gif);
lnkR.ContentId = "Pic1";
// Add the alternate views instead of using MailMessage.Body
alv.LinkedResources.Add(lnkR);
mail.AlternateViews.Add(alv);
// Send the message
// Create SmtpCline object and pass mail sever
SmtpClient client = new SmtpClient("mail.yourmailServer.net");
// send mail can be done in two different way as asynchronous and synchronous
// as syncronus follow this
client.Send(mail);
//as asyncronus
//client.SendCompleted +=new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(ms,null);
// as synchronous way u need to get status of massage after sending it using sendCompleted event.