In this code we will learn how to embed an image in mail using VB.NET.
Public Sub SendMail(ByVal message As String)
Dim vars As Variables
Dim myHtmlMessage As MailMessage
Dim mySmtpClient As SmtpClient
Dim retVal As Int32 = -1
Dim vars As Variables
Dim myHtmlMessage As MailMessage
Dim mySmtpClient As SmtpClient
Dim retVal As Int32 = -1
myHtmlMessage = New MailMessage()
Dim FromAddress As String
Dts.VariableDispenser.LockOneForWrite("from", vars)
FromAddress = vars("from").Value.ToString().Trim()
vars.Unlock()
Dts.VariableDispenser.LockOneForWrite("from", vars)
FromAddress = vars("from").Value.ToString().Trim()
vars.Unlock()
Dim ToAddress As String
Dts.VariableDispenser.LockOneForWrite("to", vars)
ToAddress = vars("to").Value.ToString().Trim()
vars.Unlock()
Dts.VariableDispenser.LockOneForWrite("to", vars)
ToAddress = vars("to").Value.ToString().Trim()
vars.Unlock()
Dim CCAddress As String
Dts.VariableDispenser.LockOneForWrite("cc", vars)
CCAddress = vars("cc").Value.ToString().Trim()
vars.Unlock()
Dts.VariableDispenser.LockOneForWrite("cc", vars)
CCAddress = vars("cc").Value.ToString().Trim()
vars.Unlock()
Dim SmtpClient As String
Dts.VariableDispenser.LockOneForWrite("smtpServer", vars)
SmtpClient = vars("smtpServer").Value.ToString().Trim()
vars.Unlock()
myHtmlMessage.From = New Mail.MailAddress(FromAddress)
Dts.VariableDispenser.LockOneForWrite("smtpServer", vars)
SmtpClient = vars("smtpServer").Value.ToString().Trim()
vars.Unlock()
myHtmlMessage.From = New Mail.MailAddress(FromAddress)
Dim tt As String()
If (ToAddress.ToString().Trim().Length > 0) Then
If (ToAddress.IndexOf(";") > 0) Then
tt = ToAddress.Split(";")
Dim c As String
If (tt.Length > 0) Then
For Each c In tt
myHtmlMessage.To.Add(c)
Next
End If
Else
myHtmlMessage.To.Add(ToAddress.ToString().Trim())
End If
End If
If (ToAddress.ToString().Trim().Length > 0) Then
If (ToAddress.IndexOf(";") > 0) Then
tt = ToAddress.Split(";")
Dim c As String
If (tt.Length > 0) Then
For Each c In tt
myHtmlMessage.To.Add(c)
Next
End If
Else
myHtmlMessage.To.Add(ToAddress.ToString().Trim())
End If
End If
Dim cc As String()
If (CCAddress.ToString().Trim().Length > 0) Then
If (CCAddress.IndexOf(";") > 0) Then
cc = CCAddress.Split(";")
Dim c As String
If (cc.Length > 0) Then
For Each c In cc
myHtmlMessage.CC.Add(c)
Next
End If
Else
myHtmlMessage.CC.Add(CCAddress)
End If
End If
If (CCAddress.ToString().Trim().Length > 0) Then
If (CCAddress.IndexOf(";") > 0) Then
cc = CCAddress.Split(";")
Dim c As String
If (cc.Length > 0) Then
For Each c In cc
myHtmlMessage.CC.Add(c)
Next
End If
Else
myHtmlMessage.CC.Add(CCAddress)
End If
End If
Dim MailSubject As String = " Email scheduling process"
Dim mailBody As String = "Hi, </BR>" + message + "</BR> Thanks, </BR> XXX"
Dim mailBody As String = "Hi, </BR>" + message + "</BR> Thanks, </BR> XXX"
Dim ApplicationPath As String ' Physical path of an image. Example: C:/images/
Dts.VariableDispenser.LockOneForWrite("ApplicationPath", vars)
ApplicationPath = vars("ApplicationPath").Value.ToString().Trim()
vars.Unlock()
Dts.VariableDispenser.LockOneForWrite("ApplicationPath", vars)
ApplicationPath = vars("ApplicationPath").Value.ToString().Trim()
vars.Unlock()
' EMBEDING IMAGE
Dim contentId As String = "image1" ' giving ID to an image content
Dim path As String = ApplicationPath + "\\myLogo.JPG"
Dim myImage As String = "<img src=""cid:image1""/><br />"
Dim mybody As String = String.Empty
Dim linkedResource As System.Net.Mail.LinkedResource = New System.Net.Mail.LinkedResource(path)
Dim av1 As System.Net.Mail.AlternateView
linkedResource.ContentId = contentId
linkedResource.ContentType.Name = path
av1 = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody + "</BR>" + myImage, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)
av1.LinkedResources.Add(linkedResource)
myHtmlMessage.AlternateViews.Add(av1)
myHtmlMessage.Subject = MailSubject
myHtmlMessage.IsBodyHtml = True
mySmtpClient = New SmtpClient(SmtpClient)
mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
Dim contentId As String = "image1" ' giving ID to an image content
Dim path As String = ApplicationPath + "\\myLogo.JPG"
Dim myImage As String = "<img src=""cid:image1""/><br />"
Dim mybody As String = String.Empty
Dim linkedResource As System.Net.Mail.LinkedResource = New System.Net.Mail.LinkedResource(path)
Dim av1 As System.Net.Mail.AlternateView
linkedResource.ContentId = contentId
linkedResource.ContentType.Name = path
av1 = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody + "</BR>" + myImage, Nothing, System.Net.Mime.MediaTypeNames.Text.Html)
av1.LinkedResources.Add(linkedResource)
myHtmlMessage.AlternateViews.Add(av1)
myHtmlMessage.Subject = MailSubject
myHtmlMessage.IsBodyHtml = True
mySmtpClient = New SmtpClient(SmtpClient)
mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
mySmtpClient.Send(myHtmlMessage)
End Sub
End Sub
Comments
Post a Comment