Windows service solution for automatic mail

Source: Internet
Author: User
Tags smtpclient

A friend asked me a few times ago to write a program to send an email to the target email address at the specified time after obtaining data locally. the following solutions are available for reference. I am also a cainiao. It is not well written. Please kindly advise. Thank you.

Build a Windows Service, read the sender information (including SMTP address, user name, and password) from the configuration file at a specified time point, and define the mail title and content, then you can send the message. Of course, you have to install the Windows Service and configure it to start automatically.

-------------------------------------

For source code download, click here

-------------------------------------

Next, we will first build a Windows Service (using a timer control to control timed refresh ):

 

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. diagnostics; using system. serviceprocess; using system. text; using system. net. mail; using system. net; using system. configuration; namespace MailServer {public partial class service1: servicebase {public service1 () {initializecomponent ();} protected override void onstart (string [] ARGs) {// when the minute is an even While (datetime. now. second % 2 = 0) {// defines the mail client smtpclient client = new smtpclient (configurationmanager. appsettings ["smtpclient"], Int. parse (configurationmanager. appsettings ["smtpprot"]); // defines the sender's email address and password client. credentials = new networkcredential (configurationmanager. deleetask[ "from_address"], configurationmanager. deleetask[ "from_password"]); // defines the sender and receiver of the email mailmessage mail = new mailmes Sage (New mailaddress (configurationmanager. deleetask[ "from_address"], configurationmanager. deleetask[ "from_displayname"]), new mailaddress (configurationmanager. appsettings ["to_address"], configurationmanager. appsettings ["to_displayname"]); // defines the mail title mail. subject = "test email [" + datetime. now. tostring () + "]"; // defines mail content. body = guid. newguid (). tostring (); // send the email client. send (Mail) ;}} protected ov Erride void onstop () {// todo: Add code here to stop the service. }}}
 
-------------------
Configuration file:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <deleetask> <add key = "smtpclient" value = "smtp.126.com"/> <add key = "smtpprot" value = "25"/> <add key = "from_address "value =" Enter your email address, for example: ivwjqrhome@126.com "/> <add key =" from_displayname "value =" mr. yang "/> <add key =" from_password "value =" Enter your email password "/> <add key =" to_address "value =" ivwjqrhome@126.com "/> <add key = "to_displayname" value = "** mr."/> </appsettings> </configuration>

-------------------------------------

For source code download, click here

-------------------------------------




To create a Windows Service installer, see ttp: // www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html.

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.