WebMail Class
Provides a way to construct and send an email message using Simple Mail Transfer Protocol (SMTP).
Assembly: System.Web.Helpers (in System.Web.Helpers.dll)
The WebMail type exposes the following members.
| Name | Description | |
|---|---|---|
|
EnableSsl | Gets or sets a value that indicates whether Secure Sockets Layer (SSL) is used to encrypt the connection when an email message is sent. |
|
From | Gets or sets the email address of the sender. |
|
Password | Gets or sets the password of the sender's email account. |
|
SmtpPort | Gets or sets the port that is used for SMTP transactions. |
|
SmtpServer | Gets or sets the name of the SMTP server that is used to transmit the email message. |
|
SmtpUseDefaultCredentials | Gets or sets a value that indicates whether the default credentials are sent with the requests. |
|
UserName | Gets or sets the name of email account that is used to send email. |
This class represents a helper, which is a component that simplifies web programming in ASP.NET Web Pages. You can use the WebMail class to send email messages from a web application.
To use the WebMail class, you must have access to an SMTP server. An SMTP server is an email server that forwards messages to the recipient’s server.
To send an email message, you must set the following values in code:
-
Set the SmtpServer property to the name of an SMTP server that you have access to.
-
Set the SmtpPort property to the port number that is used to access the SMTP server. Typically, an email client submits an email message using port 25 or port 587.
-
Optionally set the EnableSsl property to securely send the email message (if the SMTP host requires this option).
-
Set the UserName property to the user name for an SMTP server account.
-
Set the From property to the email address from which the message is sent. Typically, the From property is the same value as the UserName property.
-
Set the Password property to the password for the SMTP server account.
-
Set the to parameter of the Send(String, String, String, String, String, IEnumerable<String>, Boolean, IEnumerable<String>) method to the email address of the person you want to send the message to.
Many of the property values (like the SMTP server name and port number) are usually constant for a website. Therefore, you typically make many of these property settings only one time in the _AppStart.cshtml or _AppStart.vbhtml file that runs when the website first runs. You then do not to set them again before you call the Send(String, String, String, String, String, IEnumerable<String>, Boolean, IEnumerable<String>) method. (In contrast, the values that are likely to change for every email message are set as parameters that you pass to the Send(String, String, String, String, String, IEnumerable<String>, Boolean, IEnumerable<String>) method.) The following example shows how to set property values in the _AppStart.cshtml file.
@{
WebMail.SmtpServer = "mailserver.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = "username@example.com";
WebMail.Password = "your-password";
WebMail.From = "your-name-here@example.com";
}
In order to attach a file to the email message, you must include an array that contains the name of the files you want to attach.
The following example shows how to use the WebMail class to send an email message.
@{
var customerName = Request["customerName"];
var customerRequest = Request["customerRequest"];
try {
// Initialize WebMail helper
WebMail.SmtpServer = "your SMTP host";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = "your user name here";
WebMail.From = "your email address here";
WebMail.Password = "your accound password";
// Send email
WebMail.Send(to: "target email address here",
subject: "Help request from - " + customerName,
body: customerRequest
);
}
catch (Exception ex ) {
<text>
<b>The email was <em>not</em> sent.</b>
The code in the ProcessRequest page must provide an
SMTP server name, a user name, a password, and
a "from" address.
</text>
}
}
<!DOCTYPE html>
<html>
<head>
<title>Request for Assistance</title>
</head>
<body>
<p>Sorry to hear that you are having trouble, <b>@customerName</b>.</p>
<p>An email message has been sent to our customer service
department regarding the following problem:</p>
<p><b>@customerRequest</b></p>
</body>
</html>
The following example shows how to use the WebMail class to send an email message that contains an attached file.
@{
var customerName = Request["customerName"];
var subjectLine = Request["subjectLine"];
var fileAttachment = Request["fileAttachment"];
try {
// Initialize WebMail helper
WebMail.SmtpServer = "your-SMTP-host";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = "your-user-name-here";
WebMail.From = "your-email-address-here";
WebMail.Password = "your-account-password";
// Create array containing file name
var filesList = new string [] { fileAttachment };
// Attach file and send email
WebMail.Send(to: "target-email-address-here",
subject: subjectLine,
body: "File attached. <br />From: " + customerName,
filesToAttach: filesList);
}
catch (Exception ex) {
<text>
<b>The email was <em>not</em> sent.</b>
The code in the ProcessFile page must provide an
SMTP server name, a user name, a password, and
a "from" address.
</text>
}
}
<!DOCTYPE html>
<html>
<head>
<title>Request for Assistance </title>
</head>
<body>
<p><b>@customerName</b>, thank you for your interest.</p>
<p>An email message has been sent to our customer service
department with the <b>@fileAttachment</b> file attached.</p>
</body>
</html>