WebPages Email
ASP.NET WebPages provides the WebMail
helper for sending emails easily from your web application. It supports sending plain text or HTML emails and works with popular SMTP services like Gmail, Outlook, and custom mail servers.
Key Topics
Sending a Basic Email
Example
@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "your-email@example.com";
WebMail.Password = "your-password";
WebMail.Send(
to: "recipient@example.com",
subject: "Test Email",
body: "This is a basic email sent using WebMail.",
isBodyHtml: false
);
}
<p>Email sent successfully.</p>
Explanation: This example demonstrates sending a plain text email using the WebMail
helper, configured with SMTP server details.
Sending an HTML Email
Example
@{
WebMail.Send(
to: "recipient@example.com",
subject: "HTML Email Example",
body: "<h1>Welcome</h1><p>This is an HTML email.</p>",
isBodyHtml: true
);
}
<p>HTML Email sent successfully.</p>
Explanation: This example shows how to send an HTML email by setting the isBodyHtml
parameter to true
. The body content is written in HTML.
Sending Email with Attachments
Example
@{
WebMail.Send(
to: "recipient@example.com",
subject: "Email with Attachment",
body: "Please find the attached file.",
filesToAttach: new[] { Server.MapPath("~/App_Data/file.pdf") }
);
}
<p>Email with attachment sent successfully.</p>
Explanation: This example sends an email with an attachment by specifying the file path in the filesToAttach
parameter. The file is retrieved from the server's App_Data
folder.
Key Takeaways
- The
WebMail
helper simplifies sending plain text, HTML, and attachment-based emails. - SMTP server details must be configured for email functionality.
- HTML emails allow for rich formatting, while attachments enhance functionality for file sharing.