[ASP.NET Tutorial - WP Reference Manual 04] ASP.NET Web Pages - WebMail object WebMail object provides ASP.NET Web Pages with SMTP

ASP.NET Web Pages - WebMail Object

In ASP.NET Web Pages development, WebMailobjects are a powerful tool for sending emails. It provides a simple and intuitive interface that enables developers to easily send emails in web applications. This article details WebMailthe usage of the object, including sample code and detailed instructions.

Configure email settings

Before using WebMailobjects, you need to configure email settings. You need to add relevant configuration in the Web.config file.

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="smtp.example.com" port="587" userName="your_username" password="your_password" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

In the above example, you need to replace the following values ​​with actual configuration information:

  • from: Sender email address.
  • host: SMTP server address.
  • port: SMTP server port.
  • userName: The username of the SMTP server.
  • password: The password of the SMTP server.
  • enableSsl: Whether to enable SSL encrypted connection.

send email

WebMailThe object provides several methods to send emails, such as Sendand SendAsyncetc.

Sendmethod

Sendmethod for sending emails.

WebMail.Send(
    to: "[email protected]",
    subject: "Hello",
    body: "This is a test email."
);

In the above example, we Sendsent a test email using the method. You need to provide the recipient's email address, subject and body content as parameters.

SendAsyncmethod

SendAsyncmethod for sending emails asynchronously.

await WebMail.SendAsync(
    to: "[email protected]",
    subject: "Hello",
    body: "This is a test email."
);

In the above example, we SendAsyncsent a test email asynchronously using the method. By using awaitthe keyword, you can continue to execute subsequent code after the asynchronous operation completes.

Working with HTML and Attachments

WebMailThe object also provides functions to support HTML formatting and add attachments.

Send HTML mail

WebMail.Send(
    to: "[email protected]",
    subject: "HTML Email",
    body: "<h1>Hello</h1><p>This is a <strong>test email</strong> in HTML format.</p>",
    isBodyHtml: true
);

In the above example, we sent an email with HTML content by isBodyHtmlsetting the parameter to .true

Add attachments

var attachmentPath = Server.MapPath("~/Files/document.pdf");
WebMail.Send(
    to: "[email protected]",
    subject: "Attachment",
    body: "This email contains an attachment.",
    filesToAttach: new[] {
    
     attachmentPath }
);

In the above example, we filesToAttach sent an email with attachments by setting the parameter to an array containing the paths to the attachments.

Summarize

In this article, we took a detailed look at WebMailthe objects and their usage in ASP.NET Web Pages. By configuring email settings and using Sendand SendAsyncmethods, developers can easily send emails in web applications. In addition, we also introduced how to send emails in HTML format and add attachments. WebMailObjects provide flexible and powerful functionality that makes e-mail processing simple and efficient.

Note that in a real application, make sure to do proper validation and filtering when handling user input to prevent potential security risks.

Guess you like

Origin blog.csdn.net/qq_43797491/article/details/131337730