C # send an email

Source: Internet
Author: User
Tags mailmessage smtpclient

/* Creator: the blog of caidao jushi
* Creation date: January 1, September 05, 2014
* Function: send an email.
*
*/


Namespace net. String. consoleapplication
{
Using system;
Using system. text;
Using system. net. mail;
Using system. Collections. Generic;

/// <Summary>
/// Send an email
/// </Summary>
Public class sendmailhelper
{
// Your own email address
Public String frommail = "[email protected]";
// Your own name
Public String fromname = "menu ";
// Mail title
Public String mailtext = "";

// If there are multiple people, please match them one by one
// Email receiving mailbox
Public list <string> ltomail = new list <string> ();
// Recipient of the email
Public list <string> ltoname = new list <string> ();
// Email content
Public stringbuilder body = new stringbuilder ();
// The attachment of the email. Note the path of the file on the server.
Public list <string> lfile = new list <string> ();

Public void send ()
{
// Sender
Mailaddress from = new mailaddress (frommail );
// Email
Mailmessage mail = new mailmessage ();
// Set the title
Mail. Subject = mailtext;
Mail. From = from;

Int c = ltomail. count;
// Recipient
For (INT I = 0; I <C; I ++)
{
Mail. to. Add (New mailaddress (ltomail [I], ltoname [I]);
}
// Set the email content
Mail. Body = body. tostring ();
// Set the mail format
Mail. bodyencoding = system. Text. encoding. utf8;
// Whether it is in HTML Format
Mail. isbodyhtml = true;
// Set the mail sending level
Mail. Priority = mailpriority. High;

// Set the attachment
Int F = lfile. count;
For (Int J = 0; j <F; j ++)
{
Mail. attachments. Add (new attachment (lfile [J]);
}

Mail. deliverynotificationoptions = deliverynotificationoptions. onsuccess;

Smtpclient client = new smtpclient ();
// Set the name of the host used for SMTP transaction, and enter the IP address.
Client. Host = "smtp.qq.com ";
// Set the port for SMTP transaction. The default value is 25.
Client. Port = 25;
Client. usedefacrecredentials = false;

// Here is the real email login name and password. For example, my email address is [email protected], my username is hbgx, and my password is xgbh.
Client. Credentials = new system. net. networkcredential ("[email protected]", "123456 ");
Client. deliverymethod = smtpdeliverymethod. Network;

Client. Send (Mail );
System. Console. writeline ("sent successfully! ");
}

Public void sendemail (string strsmtpserver, string strfrom, string strfrompwd, string strto, string strsubject, string strbody)
{
System. net. Mail. smtpclient client = new smtpclient (strsmtpserver );
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential (strfrom, strfrompwd );
Client. deliverymethod = smtpdeliverymethod. Network;
System. net. Mail. mailmessage message = new mailmessage (strfrom, strto, strsubject, strbody );
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;
Client. Send (Message );
}
}
}

C # send an email

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.