protected void Button1_Click(object sender, EventArgs e)
{
MailSender.Send("lizihong3@163.com", "产品邮件", "产品内容");
}

using System;

using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Web;
using System.IO;
using System.Net;
using System.Net.Mail;

namespace Maticsoft.Common
{
public class MailSender
{
public static void Send(string server, string sender, string recipient, string subject,
string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
{
SmtpClient smtpClient = new SmtpClient(server);
MailMessage message = new MailMessage(sender, recipient);
message.IsBodyHtml = isBodyHtml;

message.SubjectEncoding = encoding;
message.BodyEncoding = encoding;

message.Subject = subject;
message.Body = body;

message.Attachments.Clear();
if (files != null && files.Length != 0)
{
for (int i = 0; i < files.Length; ++i)
{
Attachment attach = new Attachment(files[i]);
message.Attachments.Add(attach);
}
}

if (isAuthentication == true)
{
smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
SmtpConfig.Create().SmtpSetting.Password);
}
smtpClient.Send(message);

}

public static void Send(string recipient, string subject, string body)
{
Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
}

public static void Send(string Recipient, string Sender, string Subject, string Body)
{
Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
}

static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];

public void Send(string subject, string body)
{

List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
foreach (string s in toList)
{
OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);

msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);

//设置邮件正文,并指定格式为 html 格式
msg.HtmlBody = body;
//设置邮件标题
msg.Subject = subject;
//指定邮件正文的编码
msg.Charset = "gb2312";
//发送邮件
smtp.SendMail(msg);
}
}
}

public class SmtpSetting
{
private string _server;

public string Server
{
get { return _server; }
set { _server = value; }
}
private bool _authentication;

public bool Authentication
{
get { return _authentication; }
set { _authentication = value; }
}
private string _user;

public string User
{
get { return _user; }
set { _user = value; }
}
private string _sender;

public string Sender
{
get { return _sender; }
set { _sender = value; }
}
private string _password;

public string Password
{
get { return _password; }
set { _password = value; }
}
}

public class SmtpConfig
{
private static SmtpConfig _smtpConfig;
private string ConfigFile
{
get
{
string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
{
configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
}
else
{
if (!Path.IsPathRooted(configPath))
configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
else
configPath = Path.Combine(configPath, "SmtpSetting.config");
}
return configPath;
}
}
public SmtpSetting SmtpSetting
{
get
{
XmlDocument doc = new XmlDocument();
doc.Load(this.ConfigFile);
SmtpSetting smtpSetting = new SmtpSetting();
smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;

return smtpSetting;
}
}
private SmtpConfig()
{

}
public static SmtpConfig Create()
{
if (_smtpConfig == null)
{
_smtpConfig = new SmtpConfig();
}
return _smtpConfig;
}
}
}

邮件发送 emailsend .net开发的更多相关文章

  1. J2EE 邮件发送那些事儿

    距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...

  2. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  3. 【干货】.NET开发通用组件发布(二) 邮件发送组件

    组件介绍和合作开发 http://www.cnblogs.com/MrHuo/p/MrHuoControls.html 邮件发送组件 邮件发送组件采用常用的SMTP发送方式,需要添加以下格式的配置文件 ...

  4. .NET开发邮件发送功能

    .NET开发邮件发送功能 今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知 ...

  5. iOS开发-邮件发送

    Web开发的时候邮箱注册登录是必不可少的,手机号可以更换,不过相对而言,邮箱只是用于比较重要的时候用到,比如找工作的时候必填的邮箱,注册网站会员的邮箱验证.现在的手机和Web的其实操作是一样的,大多数 ...

  6. QT开发之旅四邮件发送工具

    终于有了一个晚上安静的写写程序,最近一直忙着公司商务上的事情,一直想用QT实现一个调用最底层socket通信来实现的邮件发送程序,以前用C#写过,微软都封装好的,不知道底层是如何实现的,只知道调用方法 ...

  7. 循序渐进BootstrapVue,开发公司门户网站(3)--- 结合邮件发送,收集用户反馈信息

    在我们公司门户网站里面,如果有需要,我们可以提供一个页面给用户反馈信息,以便获得宝贵的用户信息反馈或者一些产品咨询的记录,一般这个结合邮件发送到负责人的邮箱即可.本篇随笔结合后端发送邮件的操作,把相关 ...

  8. 测试开发【提测平台】分享11-Python实现邮件发送的两种方法实践

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 按照开发安排,本篇本应该是关于提测页面的搜索和显示实现,怕相似内容疲劳,这期改下内容顺序,将邮件服务的相关的提前,在之前的产品需求和原型中 ...

  9. AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )

    net所有的功能都要重新来一遍,集成众多类库,core任重道远,且发展且努力!! 我们都知道,很多的邮件发送都是基于这个SMTP协议,但现在的.net core对这方面还不太支持,所以我们选择这两个组 ...

随机推荐

  1. Mysql笔记之 -- 小试MYSQL主从配置

    mysql主从配置: 硬件: 两台服务器 1.Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-60-generic-pae i686)  2.Ubuntu 12.04.4 LT ...

  2. MYSQL 的数据读取方式

    例子: create table T(X bit(8)); insert into T (X) values(b'11111111'); select X from T; 这个时候会发现这个X 是乱码 ...

  3. makefile简单helloworld

    最近要在unix系统上开发c++应用程序,但默认情况下unix编译c++程序需要使用makefile.其实makefile语法还是比较简单,看上去有点像ant.废话不说了,直接上helloworld. ...

  4. 1396 - Most Distant Point from the Sea

    点击打开链接 题意: 按顺序给出一小岛(多边形)的点 求岛上某点离海最远的距离 解法: 不断的收缩多边形(求半平面交) 直到无限小 二分收缩的距离即可 如图 //大白p263 #include < ...

  5. iOS 面试题:OC基本概念题

    1.什么是类和对象? 类是一组具有同样特征和功能的事物的抽象 对象描写叙述了一个物体的特征和行为实现 类是对象的抽象 对象是类的实例 2.OC中定义类,创建对象,使用对象. OC中定义类分为接口部分, ...

  6. OOP中的多态

    尽管一直在说OOP,但说实话还不是真正的理解,面向对象的三个基本特性继承.封装.多态,前两个性质曾经 有接触听的比較多还好理解,以下主要介绍一下第三个特性--多态. 1. 定义     同一操作作用于 ...

  7. Google Maps 学习笔记(一)2014.06.04

    1.<body onload="加载地图的函数" onunload="GUnload()"> 2.new GMap2(container,opts) ...

  8. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  9. Webform之(简单投票)练习

    创建数据库: CREATE table DiaoYanTiMu ( Ids int primary key ,--题目代号 Title varchar() not null ,--要调查的题目 Sel ...

  10. css布局之块上下左右居中

    以下方案的通用代码: HTML code: <div class="box"> <div class="content"> <!- ...