ASP.NET Tutoria...login
ASP.NET Tutorial
author:php.cn  update time:2022-04-11 14:18:18

Web Pages Email


ASP.NET Web Pages - WebMail Helper


WebMail Helper - One of many useful ASP.NET Web Helpers.


WebMail Helper

The WebMail Helper makes it easier to send emails from web applications according to SMTP (Simple Mail Transfer Protocol).


Prerequisite: Email Support

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


First: Edit your AppStart page

If you have created the Demo application in this tutorial, then you already have a page named _AppStart.cshtml, The content is as follows:

_AppStart.cshtml

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

To start the WebMail helper, add the following WebMail attribute 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";

}

Attribute explanation:

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

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

EnableSsl: Value is true if the server uses SSL (Secure Socket Layer) encryption.

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

Password: Password for the SMTP email account.

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


Second: Create an email 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>

Input page The purpose is to collect the information and then submit the data to a new page where the information can be sent as an email.


Third: Create an email sending page

Then create a page for sending emails 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 )
{
<text>@ex</text>
}
}

think To learn more about sending email from ASP.NET Web Pages applications, see the WebMail Object Reference Manual.


php.cn