C#邮件发送

 

代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Net;  
  9. using System.Net.Mail;  
  10. using System.Net.Mime;  
  11. using System.IO;  
  12.   
  13. namespace SendMailExample  
  14. {  
  15.     public partial class FormSendMail : Form  
  16.     {  
  17.         public FormSendMail()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void FormSendMail_Load(object sender, EventArgs e)  
  23.         {  
  24.             txtSmtpServer.Text = "smtp.qq.com";  
  25.             txtSend.Text = "heuandmei@qq.com";  
  26.             txtDisplayName.Text = "kwoMan;  
  27.             txtPassword.Text = "";//密码  
  28.             txtReceive.Text = "18393899@qq.com";  
  29.             txtTitle.Text = "发信测试";  
  30.             txtBody.Text = "This is a test(测试)";  
  31.             rbtnNoSSL.Checked = true;  
  32.         }  
  33.   
  34.         private void btnAddFiles_Click(object sender, EventArgs e)  
  35.         {  
  36.             OpenFileDialog odlg = new OpenFileDialog();  
  37.             odlg.CheckFileExists = true;  
  38.             //只接收有效的文件名  
  39.             odlg.ValidateNames = true;  
  40.             //允许一次选择多个文件作为附件  
  41.             odlg.Multiselect = true;  
  42.             if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
  43.             {  
  44.                 lstFiles.Items.AddRange(odlg.FileNames);  
  45.             }  
  46.               
  47.         }  
  48.   
  49.         private void btnSend_Click(object sender, EventArgs e)  
  50.         {  
  51.             this.Cursor = Cursors.WaitCursor;  
  52.             MailMessage mail = new MailMessage();  
  53.             mail.From = new MailAddress(  
  54.                 txtSend.Text, txtDisplayName.Text, Encoding.UTF8);  
  55.             mail.To.Add(txtReceive.Text);  
  56.             mail.Subject = txtTitle.Text;  
  57.             mail.SubjectEncoding = Encoding.Default;  
  58.             mail.Body = txtBody.Text;  
  59.             mail.BodyEncoding = Encoding.Default;  
  60.             mail.IsBodyHtml = false;  
  61.             mail.Priority = MailPriority.Normal;  
  62.             //添加附件  
  63.             Attachment attachment = null;  
  64.             if (lstFiles.Items.Count > 0)  
  65.             {  
  66.                 for (int i = 0; i < lstFiles.Items.Count; i++)  
  67.                 {  
  68.                     string pathFileName = lstFiles.Items[i].ToString();  
  69.                     string extName = Path.GetExtension(pathFileName).ToLower();  
  70.                     //判断附件类型  
  71.                     if (extName == ".rar" || extName == ".zip")  
  72.                     {  
  73.                         attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip);  
  74.                     }  
  75.                     else  
  76.                     {  
  77.                         attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet);  
  78.                     }  
  79.                     ContentDisposition cd = attachment.ContentDisposition;  
  80.                     cd.CreationDate = File.GetCreationTime(pathFileName);  
  81.                     cd.ModificationDate = File.GetLastWriteTime(pathFileName);  
  82.                     cd.ReadDate = File.GetLastAccessTime(pathFileName);  
  83.                     mail.Attachments.Add(attachment);  
  84.   
  85.                 }  
  86.             }  
  87.             SmtpClient client = new SmtpClient();  
  88.             client.Host = txtSmtpServer.Text;  
  89.             client.Port = 25;  
  90.             //是否使用安全套接字层加密连接  
  91.             client.EnableSsl = rbtnUseSSL.Checked;  
  92.             //不使用默认凭证,注意此句必须放在 client.Credentials 的上面  
  93.             client.UseDefaultCredentials = false;  
  94.             client.Credentials = new NetworkCredential(txtSend.Text, txtPassword.Text);  
  95.             //邮件通过网络直接发送到服务器  
  96.             client.DeliveryMethod = SmtpDeliveryMethod.Network;  
  97.             try  
  98.             {  
  99.                 client.Send(mail);  
  100.                 MessageBox.Show("发送成功");  
  101.             }  
  102.             catch (SmtpException ex)  
  103.             {  
  104.                 MessageBox.Show("发送失败:" + ex.Message);  
  105.             }  
  106.             catch (Exception ex)  
  107.             {  
  108.                 MessageBox.Show("发送失败:" + ex.Message);  
  109.             }  
  110.             finally  
  111.             {  
  112.                 mail.Dispose();  
  113.                 client = null;  
  114.                 this.Cursor = Cursors.Default;  
  115.             }  
  116.         }  
  117.     }  
  118. }  
posted @ 2012-12-30 14:30  Net-Spider  阅读(184)  评论(0)    收藏  举报