How to send Mail using VB.NET

This code showz hot to send a mail with attachment in vb.net


Public Sub MailFormat(ByVal fromAddress As String, ByVal toAddress As String, ByVal body As String, ByVal subject As String, ByVal attachFile As String)

'Format written to send mail with attachment

Dim message As MailMessage = New MailMessage()
message.IsBodyHtml = True
message.To.Add(toAddress)
message.Subject = subject
message.From = New MailAddress(fromAddress)
message.Body = body.ToString()
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

If Not attachFile Is Nothing Then

Dim attachFiles As Attachment = New Attachment(attachFile)
message.Attachments.Add(attachFiles)

End If

Dim smtp As SmtpClient = New SmtpClient(ConfigurationManager.AppSettings(NAPResource.hostName), ConfigurationManager.AppSettings(NAPResource.portNumber))

smtp.Send(message)

End Sub


Comments

No responses found. Be the first to comment...


  • 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: