Mail Sending in VB.Net

Wednesday, February 18, 2009

*************************************************
Class Starts Here
_________________________________________



Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Text.RegularExpressions



Public Class ClsMailSender
Public seting As SettingType
Private _MMsg As Net.Mail.MailMessage
Private _EServer As Net.Mail.SmtpClient
Private _Cred As NetworkCredential

Public Function fSendMail(ByVal FromEmailid As String, ByVal Password As String, ByVal ToEmailID As String, Optional ByVal EmailSubject As String = "", Optional ByVal BodyMessage As String = "", Optional ByVal UseHTMLFormatting As Boolean = False, Optional ByVal header As String = "", Optional ByVal signature As String = " ", Optional ByVal AttachmentFileName As String = "") As Boolean
Try
Dim tempstr As String
Dim str() As String
Dim ServerName As String = ""
Dim SMTPServerName As String = ""
Dim SSL As Boolean = False

fSendMail = False
Application.DoEvents()

ToEmailID = Replace(ToEmailID, ";", ",")
_MMsg = New Net.Mail.MailMessage(FromEmailid, ToEmailID)
With _MMsg
.Subject = EmailSubject
.Body = header & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & BodyMessage & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & signature
.IsBodyHtml = UseHTMLFormatting
If AttachmentFileName <> "" Then If Dir(AttachmentFileName) <> "" Then .Attachments.Add(New System.Net.Mail.Attachment(AttachmentFileName))
.Priority = MailPriority.High
End With


Select Case _MMsg.From.Host
Case "gmail.com"
_EServer = New Net.Mail.SmtpClient("smtp.gmail.com", 587)
_EServer.EnableSsl = True
Case "yahoo.com"
_EServer = New Net.Mail.SmtpClient("smtp.mail.yahoo.com")
Case "yahoo.co.in"
_EServer = New Net.Mail.SmtpClient("smtp.mail.yahoo.com")
Case Else
tempstr = fGetSMTPFromSetting(_MMsg.From.Host, seting.mail)
If Len(Trim(tempstr)) <= 0 Then
MsgBox("SMTP Server setting is not saved for " & _MMsg.From.Host & " web server", vbInformation)

frmSetting.Show()
SMTPServerName = frmSetting.txtsmtpserver.Text
SSL = frmSetting.chksslrequired.Checked
Else
str = Split(tempstr, "^")
SSL = str(1)
SMTPServerName = str(0)
End If

_EServer = New Net.Mail.SmtpClient(SMTPServerName)
_EServer.EnableSsl = SSL
End Select





_Cred = New Net.NetworkCredential(FromEmailid, Trim(Password))
_EServer.Credentials = _Cred
_EServer.Send(_MMsg)

_MMsg.Dispose()
fSendMail = True
' Else
' MsgBox("Enter the Valid Email ID", vbInformation)
' End If

' If GiniSetting.LogLevel > 4 Then MakeLog("Email Send Sucessfully To :" & ToEmailID & ", Subject :" & EmailSubject)
Catch ex As Exception
' MakeLog("Cannot Send E-Mail, Error Number :" & Err.Number & ", Description : ", 0, "NOT Defined", "Send Mail()")
MsgBox("Error Generated :" & Err.Description, MsgBoxStyle.Exclamation)
_MMsg.Dispose()
End Try
End Function
End Class






__________________________________________
Class end Here
*****************************************************

Dim ObjMailSender As New ClsMailSender


If ObjMailSender.fSendMail(txtFrom.Text, txtPassword.Text, txtto.Text, txtsubject.Text, txtBody.Text, False, txtHeader.Text, txtsignature.Text, txtattachment.Text) Then
MsgBox("Mail Has Been Sent ", vbInformation)
Else
MsgBox("Mail can't be sent right now ,Please Try Later")
End If

0 comments

Post a Comment