没有牛B的设计模式,代码精练精练实用,功能齐全,调用简单 。。全全完完为码农考虑

  MailSmtp ms = new MailSmtp("smtp.qq.com","1215247044","xxxx");

            //可选参数
ms.SetCC("610262374@qq.com");//抄送可以多个
ms.SetBC("610262374@qq.com");//暗送可以多个
ms.SetIsHtml(true);//默认:true
ms.SetEncoding(System.Text.Encoding.UTF8);//设置格式 默认utf-8
ms.SetIsSSL(true);//是否ssl加密 默认为false //调用函数
bool isSuccess = ms.Send("1215247044@qq.com", "test", "610262374@qq.com", "哈哈", "哈哈", Server.MapPath("~/Test.dll")); //输出结果
Response.Write(ms.Result);

  

代码:

using System;
using System.IO;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Linq;
using System.Text.RegularExpressions; namespace SyntacticSugar
{
/// <summary>
/// ** 描述:邮件发送
/// ** 创始时间:2015-6-8
/// ** 修改时间:-
/// ** 作者:sunkaixuan
/// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4562147.html
/// </summary>
public class MailSmtp
{
/// <summary>
/// 设置邮件编码类型
/// </summary>
/// <param name="contentEncoding"></param>
public void SetEncoding(Encoding contentEncoding)
{
this._encoding = contentEncoding; }
/// <summary>
///设置邮件正文是否为 Html 格式
/// </summary>
/// <param name="isHtml"></param>
public void SetIsHtml(bool isHtml)
{
this._isHtml = isHtml;
}
/// <summary>
/// 抄送
/// </summary>
/// <param name="cc"></param>
public void SetCC(params string[] cc)
{
this._cc = cc;
}
/// <summary>
/// 暗送
/// </summary>
/// <param name="cc"></param>
public void SetBC(params string[] bc)
{
this._bcc = bc;
}
/// <summary>
/// 是否ssl加密
/// </summary>
/// <param name="isSSL"></param>
public void SetIsSSL(bool isSSL)
{
this._smtp.EnableSsl = isSSL;
} /// <summary>
/// 构造函数
/// </summary>
/// <param name="host"></param>
/// <param name="username">邮件账号</param>
/// <param name="password">密码</param>
public MailSmtp(string host, string username, string password)
{
this._smtp.Host = host;
this._smtp.Port = 0x19;
this._smtp.EnableSsl = false;
this._isHtml = true;
this._encoding = Encoding.UTF8;
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
this._smtp.UseDefaultCredentials = false;
}
else
{
this._smtp.Credentials = new NetworkCredential(username, password);
}
} /// <summary>
/// 发送邮件
/// </summary>
/// <param name="from">发件人邮件地址</param>
/// <param name="sender">发件人显示名称</param>
/// <param name="to">收件人地址</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件正文</param>
/// <param name="file">附件地址数组</param>
/// <returns>bool 是否成功 </returns>
public bool Send(string from, string sender, string to, string subject, string body, params string[] file)
{
return Send(from, sender, new string[] { to }, subject, body, file);
} /// <summary>
/// 发送邮件
/// </summary>
/// <param name="from">发件人邮件地址</param>
/// <param name="sender">发件人显示名称</param>
/// <param name="to">收件人地址</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件正文</param>
/// <param name="file">附件地址数组</param>
/// <returns>bool 是否成功 </returns>
public bool Send(string from, string sender, string[] to, string subject, string body, params string[] file)
{
string mailReg = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
if (to == null)
{
throw new ArgumentNullException("MailSmtp.Send.to");
} if (to.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
{
this.Result = "收件人地址不合法";
return false;
}
if (_bcc != null && _bcc.Length > 0)
{
if (_bcc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
{
this.Result = "暗送人地址不合法";
return false;
}
}
if (_cc != null && _cc.Length > 0)
{
if (_cc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))
{
this.Result = "抄送人地址不合法";
return false;
}
}
MailMessage message = new MailMessage(); // 创建一个附件对象
foreach (var r in file)
{
Attachment objMailAttachment;
objMailAttachment = new Attachment(r);//发送邮件的附件
message.Attachments.Add(objMailAttachment);
}
message.From = new MailAddress(from, sender);
message.Subject = subject;
message.SubjectEncoding = this._encoding;
message.Body = body;
message.BodyEncoding = this._encoding;
message.IsBodyHtml = this._isHtml;
message.Priority = MailPriority.Normal;
foreach (string str in to)
{
message.To.Add(str);
}
if (this._bcc != null && this._bcc.Length > 0)
{
foreach (string b in this._bcc)
{
message.Bcc.Add(b);
}
}
if (this._cc != null && this._cc.Length > 0)
{
foreach (string c in this._cc)
{
message.CC.Add(c);
}
} try
{
this._smtp.Send(message);
return true;
}
catch (Exception ex)
{ Console.WriteLine(ex.Message);
} return false;
} private SmtpClient _smtp = new SmtpClient();
private Encoding _encoding { get; set; }
private bool _isHtml { get; set; }
private string[] _cc { get; set; }
private string[] _bcc { get; set; }
/// <summary>
/// 获取发送结果,成功则为空
/// </summary>
public string Result { get; private set; }
} }

  

邮箱mail 发送类 ASP.NET C#的更多相关文章

  1. javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)

    引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...

  2. javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  3. Log4j的邮件发送类SMTPAppender改造

    在开发过程中,我们有时需要将重要的错误日志通过邮件发送给相关的责任人,这样能即时发现错误,即时解决.如使用Log4J,一般会做如下配置: log4j.rootLogger = debug,mail # ...

  4. C#中使用System.Web.Mail.MailMessage类无法CC多人的问题

    从.NET 2.0 开始,引入了一个新的类,System.Net.Mail.MailMessage.该类用来取代 .NET 1.1 时代System.Web.Mail.MailMessage类.Sys ...

  5. 解决java mail发送TXT附件被直接显示在正文中的问题

    这两天遇到一个问题,关于使用java mail发送邮件的问题. 详细是这样子的:我使用java mail发送异常报告邮件,邮件中有一个包含异常日志的附件,和关于设备信息的邮件正文.假设日志为log后缀 ...

  6. 使用Zabbix服务端本地邮箱账号发送报警邮件及指定报警邮件操作记录

    邮件报警有两种情况:1)Zabbix服务端只是单纯的发送报警邮件到指定邮箱,发送报警邮件的这个邮箱账号是Zabbix服务端的本地邮箱账号(例如:root@localhost.localdomain), ...

  7. PHP 邮件发送类

    mail.php <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件 * 需要的php扩展,sockets和Filei ...

  8. 2019-2-19-win10-uwp-客户端如何发送类到-asp-dotnet-core-作为参数

    title author date CreateTime categories win10 uwp 客户端如何发送类到 asp dotnet core 作为参数 lindexi 2019-2-19 9 ...

  9. php实现的IMEI限制的短信验证码发送类

    php实现的IMEI限制的短信验证码发送类 <?php class Api_Sms{ const EXPIRE_SEC = 1800; // 过期时间间隔 const RESEND_SEC = ...

随机推荐

  1. 【CUDA学习】共享存储器

    下面简单介绍一些cuda中的共享存储器和全局存储器 共享存储器,shared memory,可以被同一块中的所有线程访问的可读写存储器,生存期是块的生命期. Tesla的每个SM拥有16KB共享存储器 ...

  2. java中获取比毫秒更为精确的时间

    所以这里提醒做非常精确的时间统计的朋友,谨慎使用System.currentTimeMillis() . 在Java中可以通过System.currentTimeMillis()或者System.na ...

  3. Android composite adb interface

    我的平板连上电脑后,在eclipse的DDMS中查看不到.很奇怪以前不会,我以为在进程中有其他的adb.exe冲突.查看任务管理器没有看到其他adb.exe进程.然后重启eclipse也不用,重启电脑 ...

  4. 用于主题检测的临时日志(b2d5c7b3-e3f6-4b0f-bfa4-a08e923eda9b - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(1c773d57-4f35-40cf-ad62-bd757d5fcfae - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  5. 工作组环境下管理windows.

    此处指的是windows7 1.防火墙设置 开启wmi,remote admin,防火墙远程管理 可以使用命令行 netsh advfirewall export "C:\temp\WFco ...

  6. C# 修改webbrowser 的 useragent

    Also, there is a refresh option in the function (according to MSDN). It worked well for me (you shou ...

  7. 恶心的hadoop集群

    具体配置:网上一堆,我说一下我的问题好了! “完全” 分布式集群 注意地方有三点: 1.你的"master" dfs目录中的某个Id不一致,具体位置,有空我再找找.(经过我找了一下 ...

  8. npm install -g 全局安装总是出现permission权限问题的解决方案

    npm install -g 全局安装总是出现permission权限问题的解决方案 开始使用node的时候,在使用npm安装global packages时,习惯性地使用npm install -g ...

  9. Python中的模块与包

    标准库的安装路径 在import模块的时候,python是通过系统路径找到这些模块的,我们可以将这些路径打印出来: >>> pprint.pprint(sys.path) ['', ...

  10. 构建基于WinRT的WP8.1 App 01:页面导航及页面缓存模式

    本篇博文主要阐述基于Windows Runtime的Windows Phone 应用页面间导航相关知识,主要分为以下几个方面: Window.Frame和Page概览 页面间实现跳转 处理物理后退键 ...