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.