Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET Web Pages Email


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Pages - WebMail Helper


WebMail Helper - One of ASP.NET many useful web helpers.

The purpose of the WebMail helper is to make it easy to send messages, please refer to this article for detailed use.


WebMail Helper

The WebMail Helper makes it easier to send messages by sending messages from a web application under SMTP (Simple Mail Transfer Protocol Simple Message Transfer Protocol).


Prerequisite: Email support

To demonstrate how to use e-mail, we'll create an input page that lets users submit one page to another and send a message about support issues.


First: Edit your AppStart page

If you've already created a Demo application in this tutorial, you already have a page called _AppStart.cshtml that reads:

_AppStart.cshtml

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

To launch the WebMail helper, add the following WebMail property 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 = "[email protected]";
WebMail.Password = "password-goes-here";
WebMail.From = "[email protected]";

}

Property explanation:

SmtpServer: The name of the SMTP server used to send e-mail messages.

SmtpPort: The port used by the server to send SMTP transactions (e-mail messages).

EnableSsl: If the server is encrypted using SSL (Secure Socket Layer Security Layer), the value is true.

UserName: The name of the SMTP email account used to send e-mail messages.

Password: The password for the SMTP email account.

From: The e-mail message displayed in the sending address bar (usually the same as UserName).


Second: Create an e-mail input page

Next, 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>

The purpose of entering a page is to send the phone information, and then submit the data to a new page that can send the information as an e-mail message.


Third: Create an e-mail delivery page

Next, create a page to send 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>
}
}

To learn more about ASP.NET the Web Pages application, check out the WebMail Object Reference Manual.