C # send emails, which can be changed to group sending as needed ~

Source: Internet
Author: User
Tags mailmessage smtpclient

 

Let me go directly to the code.

 

 

Code:

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Drawing;

Using System. Text;

Using System. Windows. Forms;

Using System. Net;

Using System. Net. Mail;

Using System. Net. Mime;

Using System. IO;

 

Namespace SendMailExample

{

/// <Summary>

/// By Andrew

// Blog: http://blog.csdn.net/Andrew_wx

/// </Summary>

Public partial class FormSendMail: Form

{

Public FormSendMail ()

{

InitializeComponent ();

}

 

Private void FormSendMail_Load (object sender, EventArgs e)

{

TxtSmtpServer. Text = "smtp.qq.com ";

TxtSend. Text = "heuandmei@qq.com ";

TxtDisplayName. Text = "Andrew (Wang Xu )";

TxtPassword. Text = ""; // Password

TxtReceive. Text = "heuandmei@qq.com ";

TxtTitle. Text = "mail test ";

TxtBody. Text = "This is a test (test )";

RbtnNoSSL. Checked = true;

}

 

Private void btnAddFiles_Click (object sender, EventArgs e)

{

OpenFileDialog odlg = new OpenFileDialog ();

Odlg. CheckFileExists = true;

// Only valid file names are received

Odlg. ValidateNames = true;

// Multiple files can be selected as attachments at a time

Odlg. Multiselect = true;

If (odlg. ShowDialog () = System. Windows. Forms. DialogResult. OK)

{

LstFiles. Items. AddRange (odlg. FileNames );

}

}

 

Private void btnSend_Click (object sender, EventArgs e)

{

This. Cursor = Cursors. WaitCursor;

MailMessage mail = new MailMessage ();

Mail. From = new MailAddress (

TxtSend. Text, txtDisplayName. Text, Encoding. UTF8 );

Mail. To. Add (txtReceive. Text );

Mail. Subject = txtTitle. Text;

Mail. SubjectEncoding = Encoding. Default;

Mail. Body = txtBody. Text;

Mail. BodyEncoding = Encoding. Default;

Mail. IsBodyHtml = false;

Mail. Priority = MailPriority. Normal;

// Add an attachment

Attachment attachment = null;

If (lstFiles. Items. Count> 0)

{

For (int I = 0; I <lstFiles. Items. Count; I ++)

{

String pathFileName = lstFiles. Items [I]. ToString ();

String extName = Path. GetExtension (pathFileName). ToLower ();

// Determine the attachment type

If (extName = ". rar" | extName = ". zip ")

{

Attachment = new Attachment (pathFileName, MediaTypeNames. Application. Zip );

}

Else

{

Attachment = new Attachment (pathFileName, MediaTypeNames. Application. Octet );

}

ContentDisposition cd = attachment. ContentDisposition;

Cd. CreationDate = File. GetCreationTime (pathFileName );

Cd. ModificationDate = File. GetLastWriteTime (pathFileName );

Cd. ReadDate = File. GetLastAccessTime (pathFileName );

Mail. Attachments. Add (attachment );

 

}

}

SmtpClient client = new SmtpClient ();

Client. Host = txtSmtpServer. Text;

Client. Port = 25;

// Whether to use Secure Socket Layer for encrypted connection

Client. EnableSsl = rbtnUseSSL. Checked;

// Do not use the default credential. Note that this sentence must be placed on client. Credentials.

Client. usedefacrecredentials = false;

Client. Credentials = new NetworkCredential (txtSend. Text, txtPassword. Text );

// Emails are sent directly to the server over the network

Client. DeliveryMethod = SmtpDeliveryMethod. Network;

Try

{

Client. Send (mail );

MessageBox. Show ("sent successfully ");

}

Catch (SmtpException ex)

{

MessageBox. Show ("failed to send:" + ex. Message );

}

Catch (Exception ex)

{

MessageBox. Show ("failed to send:" + ex. Message );

}

Finally

{

Mail. Dispose ();

Client = null;

This. Cursor = Cursors. Default;

}

}

}

}

Copy code

 

The above is the complete code.

Project package: http://www.bkjia.com/uploadfile/2011/1210/20111210024614328.rar

 

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.