Wednesday, March 15, 2006

VB.NET: SMTP Mail

Easy SMTP function in VB.NET


Imports System.Web.Mail.SmtpMail
Imports System.Data
Imports System.Data.SqlClient

Function SendEmail(ByVal strTo As String, ByVal strFrom As String, ByVal strBody As String, ByVal strSubject As String, Optional ByVal strCC As String = "") As Boolean
Dim email As New System.Web.Mail.MailMessage()
email.To = strTo
email.Cc = strCC
email.From = strFrom
email.Body = strBody
email.Subject = strSubject
email.BodyFormat = Web.Mail.MailFormat.Text
Try
System.Web.Mail.SmtpMail.SmtpServer = "primaryserver.com"
System.Web.Mail.SmtpMail.Send(email)
Catch e As Exception
Debug.WriteLine(e)
email.Body = strBody & vbCrLf & vbCrLf & "MAIN SMTP SERVER ERROR: " & vbCrLf & e.ToString
System.Web.Mail.SmtpMail.SmtpServer = "secondary server.com"
System.Web.Mail.SmtpMail.Send(email)
End Try
Return True
End Function

No comments: