Tuesday, 16 July 2013

Send Email with image header and footer in asp.net(vb code)

I was facing problem while sending Email with images (Header & Footer) in MicrosoftOutlook . So
Today i will Explain you how to send Email with images which supports (MicrosoftOutlook , Gmail)


Imports System.Data
Imports System.Data.SqlClient

Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime



    Dim path As String
    Dim path1 As String
    Dim path2 As String
    Dim logo As LinkedResource
    Dim plainView As AlternateView
    Dim htmlBody As String
   Dim arr() As String

Protected Sub BtnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSend.Click
        Try

       
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim ds As New DataSet

  'get all email id from table

        cmd = New SqlCommand(" select * from TableName " , con)

        da = New SqlDataAdapter(cmd)
        da.Fill(ds)
            con.Close()
        If (ds.Tables(0).Rows.Count > 0) Then

                Session("EmailId") = ds.Tables(0).Rows(0)("E_MAIL").ToString()
                s = Convert.ToString(Session("EmailId"))
'Storing All email id in Array
                arr = s.Split(",")


        Else

        End If


            Mail.From = New MailAddress(ConfigurationManager.AppSettings("From1UserId").ToString())

            For Each j As String In arr
                Mail.To.Add(j.ToString())
            Next



            Mail.CC.Add(New MailAddress("Test@test.com"))

            Mail.Subject = "Test"

            Dim StringBuilder As New StringBuilder
            Dim writer As StringWriter
            writer = New StringWriter(StringBuilder)
            Dim htmlWriter As HtmlTextWriter
            htmlWriter = New HtmlTextWriter(writer)

            content1.RenderControl(htmlWriter)

            Dim content1_innerHTML = StringBuilder.ToString

            'Create two views, one text, one HTML.
            Dim palinBody As String = "Plain text content, viewable by those clients that don't support html"
            plainView = AlternateView.CreateAlternateViewFromString(palinBody, Nothing, "text/plain")

            '----this code for add image as header---'
            htmlBody +=
            "<img alt="""" hspace=0 src=""cid:uniqueId"" align=baseline border=0 >"
            '----------------------------------------'
            '-----this will add body ---'
            htmlBody += content1_innerHTML
            '---------------------------------'
            '----this code for add image as footer---'
            htmlBody += "<div style='width:700px; height: 300px;'>"
            htmlBody += " <table width='100%' style='float:right;'><tr><td style='float:right;'>"

            htmlBody += "<img alt="""" style='width:100px;height:100px;' hspace=0 src=""cid:sig"" align=right border=0 >"
            htmlBody += "</td></tr><tr><td>"
            htmlBody += "<div align=right>Authorised Signatory</div></td></tr><tr><td>&nbsp;</td></tr></table><br/>"
            htmlBody += "<img alt="""" hspace=0 src=""cid:uniqueId1"" align=baseline border=0 >"
            htmlBody += "</div>"
            '---------------------------------'

            'Create two views, one text, one HTML.
            Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")

            '-----mapping img path where the image has stored------'
            path = Server.MapPath("images/header.jpg")
            '--  Create  views of  one text, one HTML.----'
            'Dim imageView As New AlternateView(path, MediaTypeNames.Image.Jpeg)
            Dim imageView As New System.Net.Mail.LinkedResource(path)
            imageView.ContentId = "uniqueId"
            imageView.TransferEncoding = Net.Mime.TransferEncoding.Base64
            htmlView.LinkedResources.Add(imageView)

            path2 = Server.MapPath("images/Signature.jpg")
            'Dim imageView1 As New AlternateView(path1, MediaTypeNames.Image.Jpeg)

            Dim imageView2 As New System.Net.Mail.LinkedResource(path2)
            imageView2.ContentId = "sig"
            imageView2.TransferEncoding = Net.Mime.TransferEncoding.Base64
            htmlView.LinkedResources.Add(imageView2)

            path1 = Server.MapPath("images/footer.jpg")
            'Dim imageView1 As New AlternateView(path1, MediaTypeNames.Image.Jpeg)

            Dim imageView1 As New System.Net.Mail.LinkedResource(path1)
            imageView1.ContentId = "uniqueId1"
            imageView1.TransferEncoding = Net.Mime.TransferEncoding.Base64
            htmlView.LinkedResources.Add(imageView1)


            'add the views
            Mail.AlternateViews.Add(plainView)
            Mail.AlternateViews.Add(htmlView)
           
            Mail.AlternateViews.Add(plainView)
            Mail.AlternateViews.Add(htmlView)
         

            Mail.IsBodyHtml = True



            Dim SmtpServer As New SmtpClient(ConfigurationManager.AppSettings("smtp").ToString())
            mailuser = ConfigurationManager.AppSettings("user").ToString()
            mailpassword = ConfigurationManager.AppSettings("Password").ToString()
            SmtpServer.Port = 25
            SmtpServer.EnableSsl = False
            

            SmtpServer.Credentials = New System.Net.NetworkCredential(mailuser, mailpassword)
            SmtpServer.Send(Mail)
            Response.Write("<script language='javascript'>alert('Your email has been sent successfully');</script>")
           
        Catch ex As Exception

        End Try
    End Sub

No comments:

Post a Comment