Latest web development tutorials

ASP.NET Web Pages Email

WebMail helper - one of the many useful ASP.NET Web Helper.


WebMail helper

WebMail helper make it easier to send messages, which according to SMTP (Simple Mail Transfer Protocol Simple Mail Transfer Protocol) to send mail from the Web application.


Prerequisite: Email support

To demonstrate how to use e-mail, we will create an input page that allows users to submit a page to another page, and send a message regarding support issues.


First: Edit Your AppStart page

In this tutorial, if you have created a Demo application, then you already have a page called _AppStart.cshtml, as follows:

_AppStart.cshtml

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

To start the WebMail helper, to increase your page AppStart WebMail property as follows:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password-goes-here";
WebMail.From = "[email protected]";

}

Properties explained:

SmtpServer: for sending e-mail SMTP server name.

SmtpPort: SMTP server used to send the transaction (e-mail) port.

EnableSsl: If your server uses SSL (Secure Socket Layer Secure Sockets Layer) encryption, the value is true.

UserName: The name used for sending e-mail SMTP e-mail accounts.

Password: Password SMTP e-mail accounts.

From: e-mail sender's address bar (usually the same UserName).


Second: Create an e-mail input page

Then create an input page, and name it Email_Input:

Email_Input.cshtml

<!DOCTYPE html>
<html>
<body>
<h1>Request for Assistance</h1>

<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest" cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit" /></p>
</form>

</body>
</html>

Purpose input page is your phone, and then submit the data to the information can be used as a new page e-mail sent.


Third: Create an e-mail Send page

Then create a page for sending e-mail, and name it Email_Send:

Email_Send.cshtml

@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"[email protected]", subject: "Help request from - " + customerEmail, body: customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}

For more information about ASP.NET Web Pages application to send e-mail information, please consult: WebMail Object Reference .