VB.NET using SMTP protocol for sending email . SMTP stands for Simple Mail Transfer Protocol . VB.NET using System.Net.Mail namespace for send mail . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .In the following example shows how to send an email from a Gmail address.
create new class name MailMsg.vb and put bellow code in that class,create object of class like
dim objMail as new MailMsg
Send Mail using SendMail().
#Region "Import"
Imports System.Web.Mail
Imports System.Text
Imports System.IO
#End Region
Public Class MailMsg
#Region "Variable Declaration"
Private m_strMailFrom As String
Private m_strSmtpServer As String
Private m_strSmtpUName As String
Private m_strSmtpPwd As String
Private m_strMailTo As String
Private m_strMailCc As String
Private m_strMailBcc As String
Private m_strMailSubject As String
Private m_strMailBody As String
#End Region
#Region "Method"
''' <summary>
''' Method to send mail using static detail mentioned in code
''' </summary>
''' <param name="strMailSubject">Mail subject</param>
''' <param name="strMailBody">Mail body text</param>
''' <param name="strMailAttachmentFileName">Attachment file path</param>
''' <param name="strMailAttachmentFileToBeDeleted">Attachment file path which are going to be deleted after attachment</param>
''' <remarks></remarks>
Public Sub SendMail(ByVal strMailSubject As String, ByVal strMailBody As String, Optional ByVal strMailAttachmentFileName As String = "", Optional ByVal strMailAttachmentFileToBeDeleted As ArrayList = Nothing)
Try
m_strSmtpServer = "Mail ServerName"
m_strSmtpUName = "UserName"
m_strSmtpPwd = "Password"
'Mail address
m_strMailFrom = "FromEmailId"
m_strMailCc = ""
m_strMailTo = "To Email Id's (;seperated) "
m_strMailBcc = "Bcc EmailId's (;seperated) "
m_strMailSubject = strMailSubject
m_strMailBody = strMailBody
If fnCheckUserInput() = True Then
Dim objMailMessage As New System.Web.Mail.MailMessage
objMailMessage.Subject = m_strMailSubject
objMailMessage.From = m_strMailFrom
objMailMessage.To = m_strMailTo
If m_strMailCc <> "" Then
objMailMessage.Cc = m_strMailCc
End If
If m_strMailBcc <> "" Then
objMailMessage.Bcc = m_strMailBcc
End If
objMailMessage.BodyFormat = MailFormat.Html
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'Basic Authentication
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", m_strSmtpUName) 'user name
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", m_strSmtpPwd) 'password
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverpost", 25)
SmtpMail.SmtpServer = m_strSmtpServer
'Attach backup file in mail
If strMailAttachmentFileName <> "" Then
If File.Exists(strMailAttachmentFileName) Then
Dim BackUpFileAttachment As New MailAttachment(strMailAttachmentFileName)
objMailMessage.Attachments.Add(BackUpFileAttachment)
End If
End If
If Not strMailAttachmentFileToBeDeleted Is Nothing Then
For Each strFileToBeDeleted As String In strMailAttachmentFileToBeDeleted
If strFileToBeDeleted <> "" Then
If File.Exists(strFileToBeDeleted) Then
Dim BackUpFileAttachment As New MailAttachment(strFileToBeDeleted)
objMailMessage.Attachments.Add(BackUpFileAttachment)
End If
End If
Next
End If
Dim MailBodyBuilder As New StringBuilder()
MailBodyBuilder.AppendLine("<html><body style=' font-family: Verdana; font-size: 10pt;'>")
MailBodyBuilder.AppendLine(m_strMailBody)
MailBodyBuilder.AppendLine("</body></html>")
objMailMessage.Body = MailBodyBuilder.ToString
SmtpMail.Send(objMailMessage) ' Send Mail
'Delete Backup file if it has been sent in mail
If Not strMailAttachmentFileToBeDeleted Is Nothing Then
For Each strFileToBeDeleted As String In strMailAttachmentFileToBeDeleted
If strFileToBeDeleted <> "" Then
If File.Exists(strFileToBeDeleted) Then File.Delete(strFileToBeDeleted)
End If
Next
End If
End If
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' Functin to check user input
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function fnCheckUserInput() As Boolean
If m_strSmtpServer = "" Then
Dim exError As New Exception("SMTP server name is missing")
Throw exError
ElseIf m_strSmtpUName = "" Then
Dim exError As New Exception("SMTP user name is missing")
Throw exError
ElseIf m_strSmtpPwd = "" Then
Dim exError As New Exception("SMTP password is missing")
Throw exError
ElseIf m_strMailFrom = "" Then
Dim exError As New Exception("From address is missing")
Throw exError
ElseIf m_strMailTo = "" Then
Dim exError As New Exception("To address is missing")
Throw exError
ElseIf m_strMailSubject = "" Then
Dim exError As New Exception("Subject is missing")
Throw exError
ElseIf m_strMailBody = "" Then
Dim exError As New Exception("Mail body is missing")
Throw exError
End If
Return True
End Function
#End Region
End Class
0 comments:
Post a Comment