Saturday, 1 August 2015

Email Send Class in Vb.net

Imports System.Net.Mail
Public Class Emailclass
    Public Function SendEmail(ByVal Recipient As String, _
                      ByVal CopyRecipients As List(Of String), _
                      ByVal FromAddress As String, _
                      ByVal Subject As String, _
                      ByVal Body As String, _
                      ByVal UserName As String, _
                      ByVal Password As String, _
                      ByVal ishtml As Boolean, _
                      ByVal ssl As Boolean, _
                      Optional ByVal Server As String = "smtp.gmail.com", _
                      Optional ByVal Port As Integer = 587, _
                      Optional ByVal Attachments As List(Of String) = Nothing, _
                      Optional ByVal ImgCnts As List(Of String) = Nothing) As Boolean

        Dim Email As New MailMessage()

        Try
            Dim SMTPServer As New SmtpClient
            For Each Attachment As String In Attachments
                Email.Attachments.Add(New Attachment(Attachment))
            Next

            Email.From = New MailAddress(FromAddress)

            Email.To.Add(Recipient)


            For Each CopyRecipient As String In CopyRecipients
                If CopyRecipient <> "" Then
                    Email.CC.Add(CopyRecipient)
                End If
            Next
            Email.Body = Body
            If ishtml = True Then
                For Each ImgCnt As String In ImgCnts
                    If ImgCnt <> "" Then
                        Dim alternateView As AlternateView = alternateView.CreateAlternateViewFromString(Body, Nothing, "text/html")
                        Dim imgpath As String = Trim(Strings.Left(ImgCnt, InStr(ImgCnt, "|") - 1))
                        'Create the LinkedResource here
                        Dim logo As New LinkedResource(imgpath, "image/jpeg")  'Content Type is set as image/jpeg
                        logo.ContentId = Trim(Mid(ImgCnt, InStr(ImgCnt, "|") + 1))
                        logo.TransferEncoding = Net.Mime.TransferEncoding.Base64

                        alternateView.LinkedResources.Add(logo)
                        Email.AlternateViews.Add(alternateView)
                    End If
                Next
            End If
            Email.Subject = Subject
            Email.IsBodyHtml = ishtml
            SMTPServer.Host = Server
            SMTPServer.Port = Port

            SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)

            SMTPServer.EnableSsl = ssl
            SMTPServer.Send(Email)
            MessageBox.Show("email sent successfully")
            Email.Dispose()

            Return True

        Catch ex As SmtpException

            Email.Dispose()
            Return False

        Catch ex As ArgumentOutOfRangeException

            Email.Dispose()
            Return False

        Catch Ex As InvalidOperationException

            Email.Dispose()
            Return False
        End Try
    End Function

End Class

No comments:

Post a Comment