System. Net. Mail example of email sending, system.net. mail

Source: Internet
Author: User
Tags mail example mailmessage smtpclient

System. Net. Mail example of email sending, system.net. mail

The automatic mail sending program of OA is poor, and the mail sending program of SQL Server Stored Procedure is good, but it is not conducive to standardization and maintenance. Therefore, it is suitable to arm the system with "Wheels. I remembered that I had learned System. Net. Mail before, so I went over it again.

Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. net. mail; using DevComponents. dotNetBar; namespace SendMailSystem {class MailSendClass {string mto; // recipient address string mfrom; // sender address string mtitle; // mail title string mbody; // mail content string msmtp; // SMTP address string msmtpuid; // sender username string msmtppwd; // sender password string mattach; // attachment content /// <summary> // email Sending parameter value /// </summary> /// <param name = "mailto"> recipient address </param> /// <param name = "mailfrom"> sender address </param> /// <param name = "mailtitle"> email title </param> /// <param name = "mailbody"> email content </param> // /<param name = "mailattch"> attachment content </param> // <param name = "smtp"> SMTP address </param> // <param name =" smtpuid "> sender username </param> // <param name =" smtppwd "> sender password </param> public void MailSend (string mailto, string mailfrom, strin G mailtitle, string mailbody, string mailattch, string smtp, string smtpuid, string smtppwd) {mto = mailto; mfrom = mailfrom; mtitle = mailtitle; mbody = mailbody; msmtp = smtp; msmtpuid = smtpuid; msmtppwd = smtppwd; mattach = mailattch;} public void mSend () {SmtpClient client = new SmtpClient (msmtp); // instantiate Smtp sending server class client. deliveryMethod = SmtpDeliveryMethod. network; // send the email through the remote SMTP server, Here netw Ork indicates the remote SMTP server you want to use. Client. useDefaultCredentials = true; // The Smtp server needs to verify the client. credentials = new NetworkCredential (msmtpuid, msmtppwd); // verify the sender's account and password // MailMessage mmsg = new MailMessage (new MailAddress (mfrom), new MailAddress (mto )); // when a single message is sent, MailMessage mmsg = new MailMessage (); // instantiate the Smtp client class mmsg. priority = MailPriority. high; // mail priority mmsg. from = new MailAddress (mfrom, "ERP system", Encoding. getEncoding (936); // The email source seen by the recipient, You can modify the Receiving address // mmsg. CC. add (mto); // cc of an email. You can send emails in batches. Separate multiple email addresses with commas. to. add (mto); // email recipient mmsg. subject = mtitle; // mail title mmsg. subjectEncoding = Encoding. UTF8; mmsg. body = mbody; // Body mmsg. isBodyHtml = true; // whether the email body is in the HTML format of mmsg. bodyEncoding = Encoding. UTF8; mmsg. isBodyHtml = true; if (mattach! = "") {String [] arrayS = mattach. toString (). substring (0, mattach. toString (). length-1 ). split (','); for (int I = 0; I <arrayS. length; I ++) {mmsg. attachments. add (new Attachment (arrayS [I]. toString ();} try {client. send (mmsg); MessageBoxEx. show ("email sent successfully! ");} Catch (Exception e) {MessageBoxEx. Show (" failed to send email: \ n "+ e. Message );}}}}Class Codeusing System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using DevComponents. dotNetBar; namespace SendMailSystem {public partial class Form1: Office2007Form {public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {} private void bu Ttonxforwardedclick (object sender, EventArgs e) {MailSendClass classl = new MailSendClass (); string mailaddress = ""; if (listBox1.Items. count> 0) {for (int I = 0; I <listBox1.Items. count; I ++) {mailaddress + = listBox1.Items [I]. toString () + "," ;}} classl. mailSend (mailto1.Text, mailfrom1.Text, mailtitle1.Text, mailbody1.Text, mailaddress, smtp1.Text, smtpuid1.Text, smtppwd1.Text); classl. mSend ();} Private void buttonX2_Click (object sender, EventArgs e) {string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog (); openFileDialog1.InitialDirectory = "D: \ Patch "; openFileDialog1.Filter = "All files (*. *) | *. * "; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog () = DialogResult. OK) resultFile = openFileDialog1.FileName; If (resultFile! = "") {// Mattach1.Text + = resultFile + ","; listBox1.Items. add (resultFile);} // ListViewItem li = new ListViewItem (); // li. text = resultFile; // li. subItems. add (resultFile); // listViewEx1.Items. add (li);} private void buttonX3_Click (object sender, EventArgs e) {// if (mattach1.Text. toString ()! = "") // {// String [] arrayS = mattach1.Text. toString (). substring (0, mattach1.Text. toString (). length-1 ). split (','); // for (int I = 0; I <arrayS. length; I ++) // {// MessageBoxEx. show (arrayS [I]. toString (); //} if (listBox1.Items. count> 0) {string mailaddress = ""; if (listBox1.Items. count> 0) {for (int I = 0; I <listBox1.Items. count; I ++) {mailaddress + = listBox1.Items [I]. toString () + "," ;}} MessageBoxEx. show (mailaddress) ;}} private void buttonX4_Click (object sender, EventArgs e) {if (listBox1.Items. count> 0) {if (listBox1.SelectedIndex> = 0) {listBox1.Items. removeAt (listBox1.SelectedIndex) ;}} private void buttonX5_Click (object sender, EventArgs e) {if (listBox1.Items. count> 0) {listBox1.Items. clear ();}}}}View Code

Finally ,.


Help: aspnet provides an example of sending emails to QQ mail

The premise is that you must activate smtp in your mailbox.

# Region: whether the email is sent successfully
Protected void Page_Load (object sender, EventArgs e)
{
Bool bResult = SendEDMEmailBy47Server ("recipient mailbox such as: 1204472077@qq.com", "sender mailbox such as: hu_hujun@163.com", "content", "title", "Login mailbox such as: hu_hujun@163.com ", "password", Server. mapPath ("js/jquery. js "));
// This. hidBool. Value = bResult. ToString ();
If (bResult)
{
This. Response. Write ("sent successfully! ");
}
Else {
This. Response. Write ("failed to send! ");
}
}
# Endregion

# Region email sending method supports attachment
/// <Summary>
/// Send an edm email with attachments attached
/// </Summary>
/// <Param name = "StrTo"> recipient address </param>
/// <Param name = "strFrom"> sender address </param>
/// <Param name = "StrBody"> email content </param>
/// <Param name = "strSubject"> subject </param>
///// <Param name = "name"> sender address </param>
///// <Param name = "pwd"> sender password </param>
/// <Param name = "path"> attachment path </param>
/// <Returns> whether the message is sent successfully </returns>
Pub ...... the remaining full text>

C # network application programming how to send emails to multiple users

You can use the To. Add () method To Add multiple recipients. The Code is as follows:
MailMessage mailObj = new MailMessage ();
MailObj. From = new MailAddress ("admin" + "@ lisco.com.cn"); // the sender's address
MailObj. To. Add ("LZ" + "6e804" + "@ lisco.com.cn"); // recipient address
MailObj. To. Add ("LZ" + "6e769" + "@ lisco.com.cn"); // recipient address
MailObj. Subject = mail_text; // mail title
// Emails in html Format
MailObj. IsBodyHtml = true;
//// Set to advanced priority (optional or not)
// MailObj. Priority = MailPriority. High;
// Use the SmtpMail object to send an email
SmtpClient smtp = new SmtpClient ("10.128.8.11 ");
Smtp. Send (mailObj );
 

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.