This tutorial will show you how to send email from your website using c#.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="C#" %>
<%@ import Namespace="System.Net.Mail" %>
<script runat="server">
public void Page_Load(object Src, EventArgs E)
{
subPlainTextEmail();
subHtmlEmail();
}
void subPlainTextEmail()
{
string strTo,strFrom,strBody,strSubject,strBcc;
bool bolHtml = 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";// ; Separates emails
strSubject = "Subject for Plain Text email";
strBody = "To whom it may concern," + "\r"; // "\r" is used for a line break
strBody += "This is an example of a plain text email" + "\r";
strBody += "Best wishes," + "\r";
strBody += "Yourwebsite.com";
subEmail(strFrom, strTo, strBcc, strSubject, strBody, bolHtml);
}
void subHtmlEmail()
{
string strTo,strFrom,strBody,strSubject,strBcc;
bool bolHtml = 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);
}
void subEmail(System.String strFrom, System.String strTo, System.String strBcc, System.String strSubject, System.String strBody, System.Boolean bolHtml)
{
//*** Written by user CWA, CoolWebAwards.com Forums. 24 February 2010
//*** http://forum.coolwebawards.com/threads/103-Email-C
string strMailServer = "mail.yourwebsite.com";
MailMessage objMail = new MailMessage();
objMail.From = new MailAddress(strFrom);
int i;
string[] arrArray ;
arrArray = strTo.Split(';');
for (i = 0; i < arrArray.Length; i++) {
objMail.To.Add(arrArray[i]);
}
arrArray = strBcc.Split(';');
for (i = 0; i < arrArray.Length; i++) {
if (arrArray[i] != ""){
objMail.Bcc.Add(arrArray[i]);
}
}
objMail.Subject = strSubject;
objMail.Body = strBody;
objMail.IsBodyHtml = bolHtml;
SmtpClient smtp = new SmtpClient(strMailServer);
//smtp.Credentials = New System.Net.NetworkCredential("username", "password")
smtp.Send(objMail);
}
</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:
string strMailServer = "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");