14 Dec 2006

Sending Mail

First of all import these two namespaces

Imports Microsoft.VisualBasic
Imports System.Net.Mail

and then declare a global variable


Dim SendNotificationMsg As String

Function Given Below can be used to send email for many email ids on a single click



Public Function SendEMail(ByVal strFrom As String, ByVal strTo As String, ByVal strCC As String, ByVal strBcc As String, ByVal strSubject As String, ByVal strBody As String, Optional ByVal strAttachment As String = "", Optional ByVal strBodyType As String = "") As String
Try
Dim MailMsg As New MailMessage
MailMsg.From = New MailAddress(strFrom)
If strTo <> "" Then
Dim strToArray As String()
strToArray = strTo.Split(",")
Dim ToInt As Integer = strToArray.Length
Dim Tocount As Integer = 0
While Tocount < tocount =" Tocount"> "" Then
Dim strccArray As String()
strccArray = strCC.Split(",")
Dim ccInt As Integer = strccArray.Length
Dim ccCount As Integer = 0
While ccCount < cccount =" ccCount"> "" Then
Dim strBccArray As String()
strBccArray = strBcc.Split(",")
Dim BccInt As Integer = strBccArray.Length
Dim BccCount As Integer = 0
While BccCount < bcccount =" BccCount" subject =" strSubject" body =" strBody" priority =" MailPriority.High"> "" Then
Select Case LCase(strBodyType)
Case "html"
MailMsg.IsBodyHtml = True
Case "text"
MailMsg.IsBodyHtml = False
End Select
End If
If strAttachment <> "" Then
Dim strAttachmentArray As String()
strAttachmentArray = strAttachment.Split(",")
Dim AttachmentInt As Integer = strAttachmentArray.Length
Dim AttachmentCount As Integer = 0
While AttachmentCount < AttachmentInt
MailMsg.Attachments.Add(New Attachment(strAttachmentArray(AttachmentCount)))
AttachmentCount = AttachmentCount + 1
End While
End If
Dim MailServerName As String = "smtp.gmail.com"
'it can be used if u have gmail account. If you have any other mail server then specify here.
Dim mSmtpClient As New SmtpClient
mSmtpClient.Host = MailServerName
mSmtpClient.Port = 25
'port of gmail server.if you are using another server then specify port for that server
mSmtpClient.Credentials = New System.Net.NetworkCredential(strFrom, "password type here")
'If you has configure mail account at your system using MS OutLook and using the same same 'mail server then there is no need to check credential.
'mSmtpClient.EnableSsl = True
Try
mSmtpClient.Send(MailMsg)
Catch objException As Exception
SendNotificationMsg = objException.Message
End Try
mSmtpClient = Nothing
For Each aAttach As Attachment In MailMsg.Attachments
aAttach.Dispose()
Next
MailMsg.Attachments.Dispose()
MailMsg.Dispose()
MailMsg = Nothing
Catch ex As Exception
Return "error" & SendNotificationMsg
End Try
Return "Sent" & SendNotificationMsg
End Function


Although I also take this function from internet.but there was some hidden points any suspenses which are described by me in this topic.Use this function because its really very good.Send you comments about this topic.

No comments: