VB.Net : Send Email with attachment

VB.Net : Send Email with attachment


Private Function SendEmail(ByVal mailfrom As String, ByVal mailto As String, ByVal cc As String, ByVal bcc As String, ByVal subject As String, ByVal message As String, ByVal attach1 As String, ByVal attach2 As String, ByVal userid As String, ByVal pwd As String) As String
        Try
            'create the mail message
            Dim mail As New MailMessage()


            'set the addresses
            mail.From = New MailAddress(mailfrom)
            mail.To.Add(mailto)
            ' mail.CC.Add(cc)
            'mail.Bcc.Add(bcc)


            'set the content
            mail.Subject = subject
            mail.Body = message


            'add an attachment from the filesystem
            mail.Attachments.Add(New Attachment(attach1))


            'to add additional attachments, simply call .Add(...) again
            '  mail.Attachments.Add(New Attachment(attach2))
            ' mail.Attachments.Add(New Attachment("c:\temp\example3.txt"))


            'send the message
            Dim mailClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()


            'This object stores the authentication values
            Dim basicCredential As System.Net.NetworkCredential = New System.Net.NetworkCredential(userid, pwd)
            mailClient.Host = "10.0.8.220"
            mailClient.UseDefaultCredentials = False
            mailClient.Credentials = basicCredential
            mailClient.Send(mail)
            Return "ok"
        Catch exp As Exception


            Return "Error: " & exp.Message
        End Try


    End Function '


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
            Dim dt As String = Now.Day.ToString & "." & Now.Month.ToString & "." & Now.Year.ToString
            lblStatus.Text = lblStatus.Text & SendEmail("cpmc@ntpc.co.in", "vinodkotiya@ntpc.co.in", "", "", "PP&M Website Upload Status " & dt, "Please find attached herewith PP&M Website Upload Status " & dt, "E:\winapps\Emailer\Emailer\bin\Debug\pdf\upload_status.pdf", "c:\vin1.txt", "cpmc@ntpc.co.in", "c****")
       
        Me.Close()
    End Sub




- By Vinod Kotiya
www.vinodkotiya.com

Comments