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