Asp.net email sending

Source: Internet
Author: User
Tags mailmessage

Just a friend of mine sent an email in Asp.net to share it!

 

Using system;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. mail;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void btnsend_click (Object sender, eventargs E)
{

Mailmessage mailmsg;

// Create an email message
Mailmsg = new mailmessage ();
// Sending Address
Mailmsg. From = txtfromemail. Text. Trim ();
// Receiving address
Mailmsg. To = txttargetemail. Text. Trim ();;
// Set the mail body type
Mailmsg. bodyformat = mailformat. text;
// Email Subject
Mailmsg. Subject = txtsubject. Text. Trim ();]

// Create an attachment object
Mailattachment MA = new mailattachment (F. Value); // complete path of the f. Value attachment

Mailmsg. attachments. Add (MA );

// Email content
Mailmsg. Body = txtcontext. Text. Trim ();
// The IP address of the server. Because we use a local virtual SMTP server, you only need to enter the local IP address.
Smtpmail. smtpserver = "127.0.0.1 ";

 

/// Authentication is required for the following three email servers.

Mailmsg. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1 ");
// Set the authentication username (Change username to your authentication username)
Mailmsg. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "username ");
// Set the verification password (Change PWD to your verification password) // the sender's username
Mailmsg. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "PWD"); // Email Password


Smtpmail. smtpserver = "smtp.126.com"; // select another server

Smtpmail. Send (mailmsg );

}
}

Note: To set the SMTP virtual server on the local machine, the above IP address must be in the authorized IP address list. The final mail sent by the SMTP Virtual Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.