Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Web Pages Email

ASP.NET Web Pages - WebMail Helper


The WebMail helper - one of many useful ASP.NET Web helpers.


WebMail Helper

The WebMail helper makes sending email easier; it sends email from a web application using SMTP (Simple Mail Transfer Protocol).


Prerequisite: Email Support

To demonstrate how to use email, we will create an input page that lets users submit a page to another page and send an email about a support issue.


Step 1: Edit Your AppStart Page

If you have already created the Demo application in this tutorial, then you already have a page named _AppStart.cshtml with the following content:

_AppStart.cshtml

@{

WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);

}

To start the WebMail helper, add the WebMail properties shown below to your AppStart page:

_AppStart.cshtml

@{

WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);

WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "support@example.com";
WebMail.Password = "password-goes-here";
WebMail.From = "john@example.com";

}

Property explanations:

SmtpServer: The name of the SMTP server used to send email.

SmtpPort: The port the server uses to send SMTP transactions (email).

EnableSsl: True if the server uses SSL (Secure Socket Layer) encryption.

UserName: The name of the SMTP email account used to send email.

Password: The password of the SMTP email account.

From: The email displayed in the From address field (usually the same as UserName).


Step 2: Create an Email Input Page

Then create an input page and name it Email_Input:

Email_Input.cshtml

Request for Assistance

The purpose of the input page is to gather information and then submit the data to a new page that can send the information as an email.


Step 3: Create an Email Send Page

Then create a page for sending email and name it Email_Send:

Email_Send.cshtml

@{ // Read input

var customerEmail = Request["customerEmail"];

var customerRequest = Request["customerRequest"];

try

{

// Send email

WebMail.Send(to:"someone@example.com", subject: "Help request from - " + customerEmail, body: customerRequest );

}

catch (Exception ex )

{

@ex

}

}

To learn more about sending email from ASP.NET Web Pages applications, please refer to:WebMail Object Reference。