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. This 关键字和变量作用域

    public class Number {     int count; public void method01(){ //    int count=3;     count=3; //    t ...

  2. 找出n个数中出现了奇数次的两个数

    如果是找只出现了奇数次的一个数, 那么我们从头异或一遍就可以. 那么如何找出现了奇数次的两个数呢? 首先我们还是从头异或一遍, 然后结果肯定不为0, 对于异或出来的结果, 如果这个数的某一位是1, 说 ...

  3. DOCTYPE声明的几种类型

    DOCTYPE声明的几种类型 DOCTYPE 声明决定着浏览器怎么去解析和渲染当前页面,所以对于页面来说是很重要的. HTML5时代,统一用 <!DOCTYPE html> 这样简单的方式 ...

  4. oracle 11g RAC Grid Infrastructure

    grid infrastructure 软件介质下载: http://www.oracle.com/technetwork/database/database-technologies/cluster ...

  5. one Infos

    Backend Server is still running (PID:). Please try 'euc-server stop' first. (原因很简单,进程控制脚本无restart函数功 ...

  6. kbengine简单介绍(1)

    什么是kbengine? 一款开源的游戏服务端引擎,客户端通过简单的约定协议就能与服务端通讯, 使用KBEngine插件能够快速与(Unity3D, OGRE, Cocos2d-x, HTML5, 等 ...

  7. Cube Stacking(并差集深度+结点个数)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 21567   Accepted: 7554 Ca ...

  8. android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框

    本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...

  9. android播放html5视频,仅仅有声音没有图像视频

    在AndroidManifest.xm中l增加 <activity .... android:hardwareAccelerated="true" />

  10. linux 之进程间通信-------------InterProcess Communication

    进程间通信至少可以通过传送打开文件来实现,不同的进程通过一个或多个文件来传递信息,事实上,在很多应用系统里,都使用了这种方法.但一般说来,进程间 通信(IPC:InterProcess Communi ...