WebMail Class
Provides a way to construct and send an email message using Simple Mail Transfer Protocol (SMTP).
Inheritance Hierarchy
System.Object
System.Web.Helpers.WebMail
Namespace: System.Web.Helpers
Assembly: System.Web.Helpers (in System.Web.Helpers.dll)
Syntax
'Declaration
Public NotInheritable Class WebMail
'Usage
public static class WebMail
public ref class WebMail abstract sealed
[<AbstractClassAttribute>]
[<SealedAttribute>]
type WebMail = class end
public final class WebMail
The WebMail type exposes the following members.
Properties
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. |
Top
Methods
Name | Description | |
---|---|---|
Send | Sends the specified message to an SMTP server for delivery. |
Top
Remarks
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, IEnumerableString, Boolean, IEnumerableString) 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, IEnumerableString, Boolean, IEnumerableString) 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, IEnumerableString, Boolean, IEnumerableString) 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";
}
@Code
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"
End Code
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.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.