前两天用.Net写了个发送mail的util,贴上来。刚接触C#,呵呵。。。。
.net 1.1的。.net 2.0里面关于Mail的API有所变动。 .code { font-size: 12; font-family: "lucida console", sans-serif; background-color: #E1E1E1; } .com { color: #00DD00; } .nlit { color: #008800; } .slit { color: #880000; } .kw { color: #0000AA; } .norm { color: #000000; } .pp { color: #888800; }


using System;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Mail;
namespace Izhufu.utils{
  /// <summary>
  /// Summary description for SMTPClient.
  /// SMTPClient is just used for SMTPMail
  /// </summary>
  public class SMTPClient {
    private readonly string DELIMITER=";";
    private MailMessage message;
    private string[] s_cc;
    private string[] s_bcc;
    private int s_port;
    private bool s_needauth;
    private string s_username;
    private string s_password;
    //constructor
    public SMTPClient (string from, string[] to, string subject, string server) {
      message=new MailMessage();
      message.From=from;
      message.To=concatMailAddress(to, DELIMITER);
      message.Subject=subject;
      SmtpMail.SmtpServer=server;
    }

//setter for mail server port
    public void SetPort (int port){
      this.s_port=port;
    }

//setter for mail priority, for example: SetPrioprity(MailPriority.HIGH)
    public void SetPriority (MailPriority priority){
      message.Priority=priority;
    }

//setter for mail server auth configuration
    public void SetNeedAuth (bool needauth){
      this.s_needauth=needauth;
    }

//setter for mail server user name
    public void SetUserName (string username){
      this.s_username=username;
    }

//setter for mail server password
    public void SetPassword (string pwd){
      this.s_password=pwd;
    }

//setter for mail body format html?or text  for exampel: SetFormat(MailFormat.Text)
    public void SetFormat (MailFormat format){
      message.BodyFormat=format;
    }

//setter for mail body
    public void SetBody (string body){
      message.Body=body;
    }

//setter for mail body encoding, for example: SetEncoding(Encoding.ASCII
    public void SetEncoding (Encoding encoding){
      message.BodyEncoding=encoding;
    }

//add attachment to mail
    public void AddAttachment (MailAttachment attachement){
      message.Attachments.Add(attachement);
    }

//set cc 
    public void SetCC (string[] cc){
      this.s_cc=cc;
    }

//set bcc
    public void SetBcc (string[] bcc){
      this.s_bcc=bcc;
    }

//private method to concat the mail address with specified delimiter
    private string concatMailAddress (string[] addresses, string delimiter){
      if (addresses==null) return null;
      if (addresses.Length==1) return addresses[0];
      string concated="";
      for (int i=0; i<addresses.Length-1; i++) {
        concated+=addresses[i]+delimiter;
      }
      concated+=addresses[addresses.Length-1];
      return concated;
    }

//send the mail, if this mail is sent successfully return true, otherwise return false
    public bool SendMail (){
      if (s_cc!=null && s_cc.Length>0) {
        message.Cc=concatMailAddress(s_cc, DELIMITER);
      }
      if (s_bcc!=null && s_bcc.Length>0) {
        concatMailAddress(s_bcc, DELIMITER);
      }
      if (s_needauth && s_username!=null && s_password!=null) {
        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", s_username);
        message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", s_password);
      }
      try {
        SmtpMail.Send(message);
      }
      catch {
        return false;
      }
      return true;
    }

}

//End of SMTPClient
}

.Net写的Mail Util(C#)相关推荐

  1. 用MyEclipse测试发送email时报java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

    java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream的异常时, 是因为MyEclipse8.5自带的javamail版本较 ...

  2. java.lang.NoClassDefFoundError: org/apache/geronimo/mail/util/Base64Encod——解决方案

    最近在弄通过javaMail发送信息到邮箱,老是会报这样的错误:Exception in thread "main" java.lang.NoClassDefFoundError: ...

  3. com.sun.mail.util.MailConnectException: Couldn't connect to host异常解决

    最近在做一个接口项目,需要捕获所有的异常信息,并发送邮件给管理员.使用了JavaMail来实现,然后发现在本地(windows环境)上可以正常发送邮件,而放到了linux环境上就无法发送邮件.相关错误 ...

  4. 基于java mail实现简单的QQ邮箱发送邮件

    刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...

  5. Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类

    准备工作 发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务 如何开启 POP3/SMTP 服务:https://blog.csdn.net/weixin_44953227/articl ...

  6. 麦咖啡杀毒软件会阻止发送自己用程序写的邮件

    今天在写一个邮件程序: 申请了两个邮箱号: 新浪的:ge_gao_lucky@sina.com 搜狐的:ge_gao_sex@sohu.com 然后开始写程序:代码如下: package com.or ...

  7. java mail 发送qq以及网易邮件

    1.准备工作:如果是用java mail发送qq邮件 和163邮件 需要注意的地方: (1)qq.163邮箱必须生成授权码才可以发送邮件, 这里使用的QQ邮箱   我们可以在邮箱帮助中找到QQ邮箱的p ...

  8. 解决问题:javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 25;

    最近项目中需要用到发送邮件这一功能,总是遇到这样的一个报错: javax.mail.MessagingException: Could not connect to SMTP host: smtp.e ...

  9. Java使用javax.mail.jar发送邮件并同意发送附件

    因为Java在开发网页上占有绝大优势.所以作为web端的领军人物,譬如发送短信和发送邮件这些就成了必定,网络安全一再安全我们须要把账号的安全级别提到更高.因此这些对于开发者也就成了必须掌握的技能!我一 ...

最新文章

  1. 图形化编程 html,用GoJS实现图形化交互编程界面示例
  2. 为何Google将几十亿行源代码放在一个仓库?
  3. python爬取新闻并归数据库_Python爬取数据并写入MySQL数据库操作示例
  4. 思科交换机vlan配置
  5. python信号与槽_Python信号和插槽(1),python,与,一
  6. python语言入门m-Python -m参数原理及使用方法解析
  7. lvm创建逻辑卷简单过程
  8. 经验 | 深度学习如何挑选GPU?
  9. Element-UI 要怎么学?官方文档!
  10. 基于邮件推拉技术的数据库远程数据同步解决方案
  11. MAC 迅雷最新版无限重启BUG的解决方法
  12. 在linux下安装gaussian09
  13. 开关电流双线性映射无损积分器的电路结构和z变换函数
  14. 测试导航卫星软件,北斗卫星导航定位系统简介及北斗测绘测亩仪应用
  15. mysql-connector-java连接失败问题
  16. win7桌面右下角的小喇叭音量图标不见了怎么办
  17. android studio 报错 AAPT: error: style attribute ‘attr/colorPrimary (aka com.
  18. 中国移动国际mCloud体验再次升级,助力企业远程协同数字化转型
  19. 全球 26 个主流视频网站高清视频下载全搞定,包括 P 站!
  20. 有道云笔记的敏捷开发实践——好文收藏吧!【转】

热门文章

  1. 马云卸任后为物流站台 “菜鸟”将飞向何方
  2. 孙正义:未来30年的人工智能和物联网
  3. Python使用BeautifulSoup与selenium爬取Boos直聘
  4. Win10系统中临时文件夹位置及临时文件的删除
  5. 百度H5活体检测-语音校验码
  6. htc服务器更新系统,HTC U Ultra刷机教程 HTC U Ultra卡刷ruu升级更新官方系统
  7. oracle erp和金蝶,ERP和金蝶软件有什么区别!
  8. 蓝奏云第三方cmd客户端(LanZouCloud-CMD)Cookie内容设置方法
  9. 《密码朋克:自由与互联网的未来》[澳] 朱利安-阿桑奇
  10. 全国矢量shp数据:行政区划,县界,道路,河流....都可下载