Sending Email using VB January 16, 2009
Posted by mudassarkhan in VB.Net.Tags: Email using VB, Send Email using VB, smtp, System.Net, System.Net.Mail, VB.Net, VB.Net Email
trackback
Here I have discussed Sending Mails using VB.Net
First Import these Name Spaces
Imports System.Net
Imports System.Net.Mail
See the function below
Public Function SendEmail(ByVal strTo As String, ByVal strFrom As String, ByVal strSubject As String, ByVal strBody As String, ByVal strAttachmentPath As String, ByVal IsBodyHTML As Boolean) As Boolean
Dim arrToArray As String()
Dim splitter As Char = “;”
‘Loop through the recepient list and separate them
‘based on semi colon
arrToArray = strTo.Split(splitter)
Dim mm As MailMessage = New MailMessage()
mm.From = New MailAddress(strFrom)
mm.Subject = strSubject
mm.Body = strBody
‘True – HTML Body
‘False – Text Body
mm.IsBodyHtml = IsBodyHTML
‘Add the recepient email Addresses
For Each s As String In arrToArray
mm.To.Add(New MailAddress(s))
Next
‘Add Attachment
mm.Attachments.Add(New Attachment(strAttachmentPath))
Dim smtp As SmtpClient = New SmtpClient()
Try
‘Your SMTP Server
smtp.Host = “Your smtp server”
‘SSL Settings depending on your Server
smtp.EnableSsl = True / False
‘Reply To Address (Optional)
mm.ReplyTo = New MailAddress(“your reply to email”)
‘Creadentials for the Server
Dim NetworkCred As NetworkCredential
=New System.Net.NetworkCredential()
‘Your Email
NetworkCred.UserName = ” sender@abc.com”
‘Your Password
NetworkCred.Password = “your password”
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
‘Port No of the Server
smtp.Port = Your Port No ‘ Port No
smtp.Send(mm)
Return True
Catch
Return False
Finally
smtp = Nothing
mm = Nothing
End Try
End Function
To call this function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SendEmail(“sender@abc.com”, “recepient@abc.com”, “Subject”,
“Body”, “C:\MyDoc.doc”, True)
End Sub
Also these are the settings for sending email using Gmail
Host – smtp.gmail.com
SSL – true
Port No 465, 587 or just comment Port No
UserName – Gmail Email
Password Gmail Password
Comments»
No comments yet — be the first.