Sample Code for automatically sending email in the background using. net

Source: Internet
Author: User
Tags mailmessage smtpclient

Use. Net to automatically send emails in the backgroundCodeYou can save some basic information in the web. config file:

Web. config file information segment:

Code < System.net >
< Mailsettings >
< SMTP deliverymethod = " Network " From = " Email address to be sent " >
< Network host = " SMTP address of the email to be sent " Username = " Email address to be sent " Password = " Email Password " Defaultcredentials = " True " />
</ SMTP >
</ Mailsettings >
</ System.net >

Background implementation:

Code UsingSystem. net. mail;
UsingSystem. configuration;
UsingSystem. net. configuration;
UsingSystem. Web. configuration;

// Addresses for send email.
String [] Address =   New   String [] { " Recipient address 1 " , " Recipient address 2 " };
// Email body.
String Emailbody =   " Hello! Guys! " ;
// Email Subject.
String Subject =   " This is a test! " ;
// Get the config info.
Smtpsection = Netsectiongroup. getsectiongroup (webconfigurationmanager. openwebconfiguration ( " ~ /Web. config " ). Mailsettings. SMTP;
// Save the mail object.
Mailmessage mm =   New Mailmessage ();
Foreach ( String Item In Address)
Mm. to. Add (item );
Mm. From =   New Mailaddress (smtpsection. From );
Mm. bodyencoding = System. Text. encoding. utf8;
Mm. subjectencoding = System. Text. encoding. utf8;
Mm. isbodyhtml =   True ;
Mm. Body = Emailbody;
Mm. Subject = Subject;
// Set and send email.
Smtpclient SC =   New Smtpclient (smtpsection. Network. HOST );
SC. deliverymethod = Smtpdeliverymethod. Network;
SC. Credentials =   New System. net. networkcredential (smtpsection. Network. username, smtpsection. Network. Password );
SC. Send (mm );

 

 

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.