Sending email with embedded image using .net 3.5

Now throw this junk of code we can able to send email with embedded Image. Its a method you can call this method in any event

We take host name from web.config you can find the code below

protected void btnSendEmail_Click(object sender, EventArgs e)
{
if (fileImage.HasFile)
{

//Holds message information.
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
//Add basic information.
mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
mailMessage.To.Add(txtTo.Text.Trim());

mailMessage.Subject = txtSubject.Text.Trim();
//Create two views, one text, one HTML.
System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "< image src=cid:HDIImage>", null, "text/html");
//Add image to HTML version
System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);
//Add two views to message.
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
//Send message
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);

}

< Configuring with web.congif file
< system.net>
< mailSettings>
< smtp>
< network
< host="YOUR HOST HERE" />
< /smtp>
< /mailSettings>
< /system.net>

No comments: