Asp.net C # regularly send emails

Source: Internet
Author: User
Tags email account mailmessage smtpclient microsoft outlook

C # code

Protected override void onstart (string [] ARGs)
{
Mytimer ();
}

// instantiate system. timers. timer
private void mytimer ()
{< br> // set the time interval
system. timers. timer Mt = new system. timers. timer (Int. parse (configresource. interval) * 60*1000);
Mt. elapsed + = new system. timers. elapsedeventhandler (mtimedevent);
Mt. enabled = true;

}
// Construct the system. Timers. Timer instance interval event (scheduled execution event)
Private void mtimedevent (Object source, system. Timers. elapsedeventargs E)
{
// Start work
Startwork ();
}

Public void startwork ()
{
// Query the time in Table A from the database Code Omitted...
// Time comparison
If (...) // The time is later than the current system time
{
// Send an email
Int istatus = Sendmail ("Your specified recipient email address", "title", "content ");
If (istatus> 0)
{
Using (streamwriter Sw = new streamwriter (filepath + "log.txt", system. Text. encoding. getencoding ("UTF-8 ")))
{
Sw. wirte (system. datetime. Now. tostring () + "sent successfully! ")
}
}
Else {// Failed}
}
}

/// <Summary>
/// Send email
/// </Summary>
/// <Param name = "srecipientemail"> recipient address </param>
/// <Param name = "ssubject"> topic </param>
/// <Param name = "smessage"> content </param>
/// <Returns> whether the message is sent successfully </returns>
Public bool Sendmail (string srecipientemail, string ssubject, string smessage)
{

// Email object
Mailmessage emailmessage;

// SMTP client object

Smtpclient client;

// Initialize the email object

String ssenderemail = "your mailbox ";

Emailmessage = new mailmessage (ssenderemail, srecipientemail, ssubject, smessage );
Emailmessage. isbodyhtml = true;
Emailmessage. subjectencoding = system. Text. encoding. default;
Emailmessage. bodyencoding = system. Text. encoding. default;
// Add
Emailmessage. headers. Add ("X-priority", "3 ");
Emailmessage. headers. Add ("X-msmail-priority", "normal ");
Emailmessage. headers. Add ("X-mailer", "Microsoft Outlook Express 6.00.2900.2869 ");
Emailmessage. headers. Add ("X-mimeole", "produced by Microsoft mimeole v6.00.2900.2869 ");

// Email sending client
Client = new smtpclient ();

// Email server and account information
Client. Host = "email server ";
// Client. Host = "smtp.163.com ";

// Client. Port = 465;
// Client. enablessl = true;
System. net. networkcredential credential = new system. net. networkcredential ();

Credential. Username = "your email account" // you can configure it in the resource file.
Credential. Password = "password"

Client. Credentials = credential;

Try
{
Client. Send (emailmessage );
}
Catch (exception E)
{
Return false;
}
Return true;

}

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.