Asp.net send email

Source: Internet
Author: User

String file = server. mappath ("email_test.txt ");

System. Web. Mail. mailmessage MEs = new system. Web. Mail. mailmessage ();

Mes. To = "Send email ";
Mes. From = "receiving email ";

Mailattachment = new mailattachment (File );

Mes. attachments. Add (mailattachment );
Mes. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // basic Permissions
Mes. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "Mail Box Name ");
Mes. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Send email password ");
Mes. bodyencoding = system. Text. encoding. utf8;

Mes. Body = "email body email content ";

// You can select the text rendering format below

Mes. bodyformat = mailformat. html;

Mes. Subject = "mail sending test ";

Smtpmail. smtpserver = "smtp.163.com ";
Smtpmail. Send (MES );

Response. Write ("sent successfully! ");

}
Catch (exception ex)
{
Response. Write (ex. Message );
}

// The following is an email sent by the POP3 protocol of the server.

Try
{

String file = server. mappath ("email_test.txt ");

Mailaddress from = new mailaddress ("zhengpengfei0791@163.com"); // instance a mail address
Mailaddress to = new mailaddress ("solomon_msn@live.cn"); // instance a mail receipt address

Mailmessage MEs = new mailmessage (from, );

Attachment myattachment = new attachment (file, system. net. Mime. mediatypenames. application. octet );

System. net. Mime. contentdisposition disposition = myattachment. contentdisposition;

Disposition. creationdate = system. Io. file. getcreationtime (File );
Disposition. modificationdate = system. Io. file. getlastwritetime (File );
Disposition. readdate = system. Io. file. getlastaccesstime (File );

Mes. attachments. Add (myattachment );
Mes. isbodyhtml = true;

Mes. Subject = "mail test ";
Mes. Subject = "email body email content

Smtpclient client = new smtpclient ();
Client. Host = request. userhostaddress;
Client. Port = 25;

Client. Credentials = new system. net. networkcredential ("toemail", "test ");

Client. Send (MES );

Response. Write ("email sent successfully! ");

}< br> catch (exception ex)
{< br> response. Write (ex. Message);
}

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.