Sending mail with image Embed in HTML

1.Add one web form
2.Place a button
3.Copy and paste the below code in button_click
4.Add a folder to soltion explorer and name it as "Images"
5.Place an image in that folder


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg As New MailMessage("ramya.r@shriram.com", "ramya.r@shriram.com")

msg.Subject = "This is my first email through program"
msg.IsBodyHtml = True

Dim smtpClient As New SmtpClient()
Dim View As AlternateView
Dim resource As LinkedResource
Dim msgText As New StringBuilder

msgText.Append("<p>Hi there,<p>")
msgText.Append("<p>Welcome to the new world programming.<p><br>")
msgText.Append("<p>Thanks<p>")
msgText.Append("<p>With regards,<p><br>")
msgText.Append("<img src=cid:Image1 />")

'create an alternate view for your mail
View = AlternateView.CreateAlternateViewFromString(msgText.ToString(), Nothing, "text/html")

'link the resource to embedd
resource = New LinkedResource((Server.MapPath("Sunset.jpg")))

'name the resource
resource.ContentId = "Image1"

'add the resource to the alternate view
View.LinkedResources.Add(resource)

'add the view to the message
msg.AlternateViews.Add(View)
smtpClient.Host = "< SMTP SERVER NAME >"
smtpClient.Port = 25
smtpClient.UseDefaultCredentials = True
smtpClient.Credentials = New System.Net.NetworkCredential("< USER NAME >", "< PASSWORD >")
smtpClient.Send(msg)
End Sub


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: