C#写的邮箱发送程序源代码

 

C#写的邮箱发送程序源代码---支持主题、正文、附件的编辑和添加!

  270人阅读  评论(0)  收藏  举报
[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Net.Mail;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11.   
  12. namespace CSEmailTest  
  13. {  
  14.     public partial class SendEmail : Form  
  15.     {  
  16.         public SendEmail()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void btnOpen_Click(object sender, EventArgs e)  
  22.         {  
  23.             openFileTest.InitialDirectory = "D:\\";  
  24.             openFileTest.Filter = "all files (*.*)|*.*";  
  25.             openFileTest.ShowDialog();  
  26.             MessageBox.Show(openFileTest.FileName.Trim());  
  27.             listAttachment.Items.Add(openFileTest.FileName.Trim());  
  28.         }  
  29.   
  30.         private void listAttachment_KeyDown(object sender, KeyEventArgs e)  
  31.         {  
  32.             if (e.KeyCode == Keys.Delete)  
  33.             {  
  34.                 for (int i = listAttachment.SelectedItems.Count - 1; i > -1; i--)  
  35.                 {  
  36.                     listAttachment.Items.Remove(listAttachment.SelectedItems[i]);  
  37.                 }  
  38.             }  
  39.         }  
  40.   
  41.         private void btnSend_Click(object sender, EventArgs e)  
  42.         {  
  43.             Email email = new Email();  
  44.             string  orgialEmail=this.txtSendEmail.Text.Trim();  
  45.             email.mailFrom = orgialEmail;  
  46.             email.mailPwd = this.txtPwd.Text.Trim();  
  47.             email.mailSubject =this.txtMySuject.Text.Trim();  
  48.             email.mailBody = this.txtBody.Text.Trim();  
  49.             email.isbodyHtml = true;    //是否是HTML  
  50.   
  51.             int index =orgialEmail.LastIndexOf("@");  
  52.               
  53.           //  email.host = "smtp."+orgialEmail.Substring(index+1);//如果是QQ邮箱则:smtp:qq.com,依次  
  54.             email.host = "smtp.163.com";  
  55.             List<string> listEmail = new List<string>();  
  56.             foreach (var item in listToEmail.Items)  
  57.              {  
  58.                listEmail.Add(item.ToString());  
  59.              }  
  60.             email.mailToArray = listEmail.ToArray();//接收者邮件集合  
  61.   
  62.             List<string> listCCEmail= new List<string>();  
  63.             foreach (var item in listCC.Items)  
  64.             {  
  65.                 listCCEmail.Add(item.ToString());  
  66.             }  
  67.             email.mailCcArray = listCCEmail.ToArray();//抄送者邮件集合  
  68.   
  69.   
  70.   
  71.             List<string> listAtt = new List<string>();  
  72.             foreach (var item in listAttachment.Items)  
  73.             {  
  74.                 listAtt.Add(item.ToString());  
  75.             }  
  76.             email.attachmentsPath = listAtt.ToArray();//附件集合  
  77.   
  78.             if (email.Send())  
  79.             {  
  80.                 MessageBox.Show("邮件发送成功~");  
  81.             }  
  82.             else  
  83.             {  
  84.                 MessageBox.Show("邮件发送失败~");  
  85.             }  
  86.         }  
  87.   
  88.         private void btnAddToEmail_Click(object sender, EventArgs e)  
  89.         {  
  90.             listToEmail.Items.Add(this.txtToEmail.Text.Trim());  
  91.         }  
  92.   
  93.         private void btnCC_Click(object sender, EventArgs e)  
  94.         {  
  95.             listCC.Items.Add(this.txtCCEmail.Text.Trim());  
  96.         }  
  97.   
  98.   
  99.   
  100.     }  
  101.   
  102.   
  103.   
  104.     public class Email  
  105.     {  
  106.         /// <summary>  
  107.         /// 发送者  
  108.         /// </summary>  
  109.         public string mailFrom { getset; }  
  110.   
  111.         /// <summary>  
  112.         /// 收件人  
  113.         /// </summary>  
  114.         public string[] mailToArray { getset; }  
  115.   
  116.         /// <summary>  
  117.         /// 抄送  
  118.         /// </summary>  
  119.         public string[] mailCcArray { getset; }  
  120.   
  121.         /// <summary>  
  122.         /// 标题  
  123.         /// </summary>  
  124.         public string mailSubject { getset; }  
  125.   
  126.         /// <summary>  
  127.         /// 正文  
  128.         /// </summary>  
  129.         public string mailBody { getset; }  
  130.   
  131.         /// <summary>  
  132.         /// 发件人密码  
  133.         /// </summary>  
  134.         public string mailPwd { getset; }  
  135.   
  136.         /// <summary>  
  137.         /// SMTP邮件服务器  
  138.         /// </summary>  
  139.         public string host { getset; }  
  140.   
  141.         /// <summary>  
  142.         /// 正文是否是html格式  
  143.         /// </summary>  
  144.         public bool isbodyHtml { getset; }  
  145.   
  146.         /// <summary>  
  147.         /// 附件  
  148.         /// </summary>  
  149.         public string[] attachmentsPath { getset; }  
  150.   
  151.         public bool Send()  
  152.         {  
  153.             //使用指定的邮件地址初始化MailAddress实例  
  154.             MailAddress maddr = new MailAddress(mailFrom);  
  155.             //初始化MailMessage实例  
  156.             MailMessage myMail = new MailMessage();  
  157.   
  158.   
  159.             //向收件人地址集合添加邮件地址  
  160.             if (mailToArray != null)  
  161.             {  
  162.                 for (int i = 0; i < mailToArray.Length; i++)  
  163.                 {  
  164.                     myMail.To.Add(mailToArray[i].ToString());  
  165.                 }  
  166.             }  
  167.   
  168.             //向抄送收件人地址集合添加邮件地址  
  169.             if (mailCcArray != null)  
  170.             {  
  171.                 for (int i = 0; i < mailCcArray.Length; i++)  
  172.                 {  
  173.                     myMail.CC.Add(mailCcArray[i].ToString());  
  174.                 }  
  175.             }  
  176.             //发件人地址  
  177.             myMail.From = maddr;  
  178.   
  179.             //电子邮件的标题  
  180.             myMail.Subject = mailSubject;  
  181.   
  182.             //电子邮件的主题内容使用的编码  
  183.             myMail.SubjectEncoding = Encoding.UTF8;  
  184.   
  185.             //电子邮件正文  
  186.             myMail.Body = mailBody;  
  187.   
  188.             //电子邮件正文的编码  
  189.             myMail.BodyEncoding = Encoding.Default;  
  190.   
  191.             myMail.Priority = MailPriority.High;  
  192.   
  193.             myMail.IsBodyHtml = isbodyHtml;  
  194.   
  195.             //在有附件的情况下添加附件  
  196.             try  
  197.             {  
  198.                 if (attachmentsPath != null && attachmentsPath.Length > 0)  
  199.                 {  
  200.                     Attachment attachFile = null;  
  201.                     foreach (string path in attachmentsPath)  
  202.                     {  
  203.                         attachFile = new Attachment(path);  
  204.                         myMail.Attachments.Add(attachFile);  
  205.                     }  
  206.                 }  
  207.             }  
  208.             catch (Exception err)  
  209.             {  
  210.                 throw new Exception("在添加附件时有错误:" + err);  
  211.             }  
  212.   
  213.             SmtpClient smtp = new SmtpClient();  
  214.             //指定发件人的邮件地址和密码以验证发件人身份  
  215.             smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);  
  216.   
  217.   
  218.             //设置SMTP邮件服务器  
  219.             smtp.Host = host;  
  220.   
  221.             try  
  222.             {  
  223.                 //将邮件发送到SMTP邮件服务器  
  224.                 smtp.Send(myMail);  
  225.                 return true;  
  226.   
  227.             }  
  228.             catch (System.Net.Mail.SmtpException ex)  
  229.             {  
  230.                 return false;  
  231.             }  
  232.   
  233.         }  
  234.     }  
  235. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值