首先是邮件帮助类
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Net.Mail;
  using System.Windows.Forms;
  namespace zzEmail
  {
  //邮件帮助类
  class MailHelper
  {
  SmtpClient smtpClient;
  //邮件实体类,包含用户名密码
  MailModel mail;
  UserModel to;
  public MailHelper()
  {
  }
  public MailHelper(MailModel mail, UserModel t)
  {
  smtpClient = new SmtpClient();
  this.mail = mail;
  this.to = t;
  }
  public void send()
  {
  MailMessage msg=null;
  smtpClient.Credentials = new System.Net.NetworkCredential(mail.from.Send_Address.Address, mail.from.password);//设置发件人身份的票据
  smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  smtpClient.Host = "smtp." + mail.from.Send_Address.Host;
  try
  {
  msg = mail.GetMail();
  msg.To.Add(to.Send_Address.Address);
  smtpClient.Send(msg);
  MessageBox.Show("发送成功");
  }
  catch (SmtpException err)
  {
  //如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器
  if (err.StatusCode == SmtpStatusCode.GeneralFailure)
  {
  try
  {
  //有些邮件服务器不加smtp.前缀
  smtpClient.Host = null;
  smtpClient.Send(mail.GetMail());
  }
  catch (SmtpException err1)
  {
  MessageBox.Show(err1.Message, "发送失败");
  }
  }
  else
  {
  MessageBox.Show(err.Message, "发送失败");
  }
  }
  finally
  {
  //及时释放占用的资源
  msg.Dispose();
  }
  }
  }
  }
  邮件实体类
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Net.Mail;
  namespace zzEmail
  {
  //邮件实体类
  class MailModel
  {
  //主题
  public string Subject { get;set;}
  public string SubjectEncoding { get; set; }
  //内容
  public string Body { get;set;}
  public string BodyEncoding { get; set; }
  //附件
  public List<Attachment> Attachments=new List<Attachment>();
  public MailMessage message;
  //发送人
  public UserModel from;
  public MailModel(string subject,string body,UserModel f)
  {
  message = new MailMessage();
  this.Subject = subject;
  this.Body = body;
  this.from = f;
  }
  //添加附件
  public void AddAttach(Attachment file)
  {
  Attachments.Add(file);
  }
  //添加一群附件
  public void AddAttach(List<Attachment> files)
  {
  foreach (Attachment item in files)
  {
  if(item!=null)
  this.Attachments.Add(item);
  }
  }
  //返回邮件实体
  public MailMessage GetMail()
  {
  if (this.message != null)
  {
  message.Subject = this.Subject;
  message.SubjectEncoding = System.Text.Encoding.UTF8;
  message.Body = this.Body;
  message.BodyEncoding = System.Text.Encoding.UTF8;
  message.From = from.Send_Address;//设置发邮件人地址
  foreach (Attachment item in Attachments)
  {
  if (item != null)
  this.message.Attachments.Add(item);
  }
  return message;
  }
  else
  return null;
  }
  }
  }
  邮件的用户类
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Net.Mail;
  namespace zzEmail
  {
  //接收邮件的用户实体类
  class UserModel
  {
  public string nickname { get; set; }
  public string password { get; set; }
  public MailAddress Send_Address{get;set;}
  public UserModel(string useraddr)
  {
  Send_Address = new MailAddress(useraddr);
  }
  public UserModel(string useraddr,string nickname)
  {
  this.nickname = nickname;
  Send_Address = new MailAddress(useraddr);
  }
  }
  }
  使用多线程来对邮件进行发送
  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.Mail;
  using System.Threading;
  namespace zzEmail
  {
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }
  private void btn_addFile_Click(object sender, EventArgs e)
  {
  OpenFileDialog myOpenFileDialog = new OpenFileDialog();
  myOpenFileDialog.CheckFileExists = true;
  //只接收有效的文件名托福答案
  myOpenFileDialog.ValidateNames = true;
  //允许一次选择多个文件作为附件
  myOpenFileDialog.Multiselect = true;
  myOpenFileDialog.ShowDialog();
  if (myOpenFileDialog.FileNames.Length > 0)
  {
  FileList.Items.AddRange(myOpenFileDialog.FileNames);
  }
  }
  private void btn_Send_Click(object sender, EventArgs e)
  {
  UserModel from = new UserModel(txt_UserName.Text);
  from.password = txt_Pass.Text;
  UserModel to = new UserModel(txt_rec.Text);
  MailModel mail = new MailModel(txt_sub.Text, txt_content.Text, from);
  List<Attachment> filelist = new List<Attachment>();
  //添加附件托福答案
  if (FileList.Items.Count > 0)
  {
  for (int i = 0; i < FileList.Items.Count; i++)
  {
  Attachment attachFile = new Attachment(FileList.Items[i].ToString());
  filelist.Add(attachFile);
  }
  }
  mail.AddAttach(filelist);//添加附件
  MailHelper helper = new MailHelper(mail, to);
  //启动一个线程发送邮件
  Thread mythread = new Thread(new ThreadStart(helper.send));
  mythread.Start();
  }
  }
  }

C#实现对邮件的发送的更多相关文章

  1. (转载)JavaWeb学习总结(五十一)——邮件的发送与接收原理

    博客源地址:http://www.cnblogs.com/xdp-gacl/p/4209586.html 一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电 ...

  2. zabbix监控系列(4)之zabbix报警邮件无法发送

    情况介绍 首先确保邮箱规则没有把报警邮件作为垃圾邮件拉黑了. 服务器断电重启后,发现zabbix报警邮件无法发送,断电之前是好好的,但是重启后不行了,于是查看maillog日志,发现这个错误: Hos ...

  3. JavaWeb学习总结(五十一)——邮件的发送与接收原理

    一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...

  4. PHP 错误与异常 笔记与总结(7)将错误日志以邮件方式发送

    当系统发生了很严重的问题,需要立刻发送给管理员.可以通过 error_log() 将错误以邮件形式发送到邮箱. 在 php.ini 中设置: sendmail_from = 472323087@qq. ...

  5. Activation successful 数据库邮件无法发送

    问题现象: 配置好数据库邮件后发送测试邮件. 在数据库邮件发送日志中显示状态为:Activation successful.而邮件是无法收到的. 解决方法: 本次解决是将SQL Server Agen ...

  6. C#中邮件的发送基本操作

    本地配置的邮箱:http://localhost:6080/index.php //邮件的收发需要用到两个类   //1.用来创建一封邮件对象     //1.MailMessage 添加对 usin ...

  7. 利用工具MailUtils实现邮件的发送,遇到的大坑,高能预警!!

    java实现邮件的发送依赖的jar包有两个:mail.jar和activation.jar,我也找到了一个工具包:itcast-tools-1.4.jar,实现原理大家可以查看源码,先放出资源链接 h ...

  8. NodeJs之邮件(email)发送

    NodeJs之邮件(email)发送 一,介绍与需求 1.1,介绍 1,Nodemailer简介 Nodemailer是一个简单易用的Node.js邮件发送插件 github地址 Nodemailer ...

  9. 使用JavaMail发送邮件-从FTP读取图片并添加到邮件正文发送

    业务分析: 最近工作需要,需要从FTP读取图片内容,添加到邮件正文发送.发送邮件正文,添加附件采用Spring的MimeMessageHelper对象来完成,添加图片也将采用MimeMessageHe ...

随机推荐

  1. 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站

    阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房) - agapple - ITeye技术网站 阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房)

  2. 320. Generalized Abbreviation

    首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...

  3. ng-select ng-options ng-repeat的用法与区别

    最近在用angularJS里的ng-select,ng-options,ng-repeat,发现有两点不太方便: 1.当数据复杂时,循环起来比较麻烦 2.首选项如果不设置,就会为空 整理一下它们的用法 ...

  4. Adobe Dreamweaver CS6安装步骤

    dreamweaver cs6 下载地址: http://bbs.phonegap100.com/thread-135-1-1.html dreamweaver cs6 是世界顶级软件厂商adobe推 ...

  5. phpmailer邮件类下载(转)

    PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...

  6. C 语言中 typeof keyword简单介绍

    原文:http://hi.baidu.com/leowang715/blog/item/b0b96d6f972e7dd080cb4a06.html typeofkeyword是C语言中的一个新扩展.仅 ...

  7. MySQL Error Handling in Stored Procedures---转载

    This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in store ...

  8. 面试时,问哪些问题能试出一个Android应用开发者真正的水平?

    一般面试时间短则30分钟,多则1个小时,这么点时间要全面考察一个人难度很大,需要一些技巧,这里我不局限于回答题主的问题,而是分享一下我个人关于如何做好Android技术面试的一些经验: 面试前的准备 ...

  9. php 编译安装的一个 configure 配置

    yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel f ...

  10. junit 测试注解

    * @Test: 将一个 普通的方法修饰成为一个测试方法* @BeforeClass: 他会在所有的方法运行前被执行,static修饰* @AfterClass 他会在所有方法运行结束后被执行,sta ...