This tutorial will show you how to send email from your website using vb.net in plain text or HTML format. Below you will find all the functionality and code you need to do this in the simplest format.
The following code is an example of the complete working project
Code:
<%@ Page Language="VB" %>
<%@Import Namespace="System.Net.Mail" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
subHtmlEmail()
subPlainTextEmail()
End sub
Sub subPlainTextEmail()
Dim strTo, strFrom, strBody, strSubject, strBcc As String
Dim bolHtml as boolean = false ' set the body content to plain text(false) or HTML(true)
strFrom = "info@yourwebsite.com"
strTo = "email1@theirdomain.com" ' ; Separates emails
strBcc = "email2@theirdomain.com; email3@theirdomain.com; email4@theirdomain.com" ' ; seperates emails
strSubject = "Subject for Plain Text email"
strBody = "To whom it may concern," & vbCrLf & vbCrLf ' & vbCrLf is used for a line break
strBody &= "This is an example of a plain text email" & vbCrLf & vbCrLf
strBody &= "Best wishes," & vbCrLf
strBody &= "Yourwebsite.com"
subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml)
End Sub
Sub subHtmlEmail()
Dim strTo, strFrom, strBody, strSubject, strBcc As String
Dim bolHtml as boolean = true ' set the body content to plain text(false) or HTML(true)
strFrom = "info@yourwebsite.com"
strTo = "email1@theirdomain.com" ' ; Separates emails
strBcc = "email2@theirdomain.com; email3@theirdomain.com; email4@theirdomain.com" ' ; Separates emails
strSubject = "Subject for HTML email"
strBody = "<html><head></head><body>"
strBody &= "To whom it may concern,<br><br>"
strBody &= "This is an example of an HTML format email<br><br>"
strBody &= "<img src=""http://www.coolwebawards.com/image/forum_logo.gif"" /><br><br>"
strBody &= "Best wishes,<br>"
strBody &= "Yourwebsite.com"
strBody &= "</body></html>"
subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml)
End Sub
Sub subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml)
'*** Written by user CWA, CoolWebAwards.com Forums. 24 February 2010
'*** http://forum.coolwebawards.com/threads/102-Email-VB
Dim strMailServer As String = "mail.yourwebsite.com"
Dim objMail As New MailMessage()
objMail.From = New MailAddress(strFrom)
dim i as integer
dim arrArray AS Array
arrArray=Split(strTo, ";")
For i = 0 to arrArray.Length - 1
objMail.To.Add(arrArray(i))
Next
arrArray=Split(strBcc, ";")
For i = 0 to arrArray.Length - 1
If not arrArray(i) = "" Then objMail.Bcc.Add(arrArray(i))
Next
objMail.Subject = strSubject
objMail.Body = strBody
objMail.IsBodyHtml = bolHtml
Dim smtp As New SmtpClient(strMailServer)
'smtp.Credentials = New System.Net.NetworkCredential("username", "password")
smtp.Send(objMail)
End Sub
</script>
Setup
1. Variables to be passed to “subEmail”
strFrom – The email address the email is sent from
strTo – The email address that the email will be sent to. Use “;” to separate emails is you like to add more than one
strBcc – Send a Blind Carbon Copy (Bcc) to these email address/es. Use “;” to separate emails is you like to add more than one
strSubject – Email subject line
strBody – The body text for your email
bolHtml – Whether or not the email body is HTML (true) or plain text (false)
2. Change the following line to your mail server address
Code:
Dim strMailServer As String = "mail.yourwebsite.com"
3. If your mail server requires authentication, you can uncomment the following line to pass the username and password values which must also be setup on your mail server
Code:
smtp.Credentials = New System.Net.NetworkCredential("username", "password")