邮件发送(C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
namespace Haitai
{
public class EmailClient
{
public EmailClient(string host, int port, bool enableSsl, string username, string password)
{
_host = host;
_port = port;
_enableSsl = enableSsl;
_username = username;
_password = password;
}
public event SendCompletedEventHandler SendCompleted;
private string _host;
private int _port;
private bool _enableSsl;
private string _username;
private string _password;
private SmtpClient _smtpClient;
private SmtpClient SmtpClient
{
get
{
if (_smtpClient == null)
{
_smtpClient = new SmtpClient();
_smtpClient.Host = _host;
_smtpClient.Port = _port;
if (_username != null && _password != null)
{
_smtpClient.Credentials = new NetworkCredential(_username, _password);
}
_smtpClient.EnableSsl = _enableSsl;
_smtpClient.SendCompleted += (sender, e) =>
{
if (SendCompleted != null)
{
SendCompleted(this, e);
}
};
}
return _smtpClient;
}
}
private MailMessage ComposeMessage(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from, fromName, Encoding.UTF8);
foreach (string to in toList.Keys)
{
message.To.Add(new MailAddress(to, toList[to], Encoding.UTF8));
}
if (ccList != null)
{
foreach (string cc in ccList.Keys)
{
message.CC.Add(new MailAddress(cc, ccList[cc], Encoding.UTF8));
}
}
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
if (attachments != null)
{
foreach (string name in attachments.Keys)
{
message.Attachments.Add(new Attachment(attachments[name], name));
}
}
return message;
}
public void Send(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
Send(from, fromName, toList, null, subject, body, null);
}
public void Send(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
SmtpClient.Send(ComposeMessage(from, fromName, toList, ccList, subject, body, attachments));
}
public MailMessage SendAsync(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
return SendAsync(from, fromName, toList, null, subject, body, null);
}
public MailMessage SendAsync(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = ComposeMessage(from, fromName, toList, ccList, subject, body, attachments);
SmtpClient.SendAsync(message, message);
return message;
}
}
}
邮件发送(C#)的更多相关文章
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- J2EE 邮件发送那些事儿
距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...
- 结合ABP源码实现邮件发送功能
1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...
- SSH项目里面 忘记密码的邮件发送功能
package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...
- [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作
这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...
- java spring 邮件发送
开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...
- Java邮件发送与接收原理
一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...
- c#实现邮件发送链接激活
2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...
- .Net(C#)最简单的邮件发送案例
一.序言 刚开始接触邮件发送功能的时候,在网上找的资料都挺复杂的!对于新手入门有点难(至少对于本人来说,第一次接触的时候确实不容易).这里就写一段简单的邮箱发送代码,备忘,也给新手一个参考(相关类的字 ...
- SpringMVC 邮件发送
<!--邮件发送实现类--> <bean id="javaMailSender" class="org.springframework.mail.jav ...
随机推荐
- redis keys 命令
## 删除存在的key del key ## 序列体弱给定key,并返回被序列化的值 dump key ## 检查key是否存在 exists key ## 为给定key设置过期时间 expire k ...
- android 实现 view 滑动
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 1,通过view 的 滑动到 方法 或者 通过什么滑动 方法 实现. 适合 视图 ...
- 【洛谷】2602: [ZJOI2010]数字计数【数位DP】
P2602 [ZJOI2010]数字计数 题目描述 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 输入输出格式 输入格式: 输入文件中仅包含一行两个整数a ...
- HDU 5694 BD String 迭代
BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...
- Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞
B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...
- List常用子类的特点
ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全, 效率较高 Vector 底层数据结构是数组,查询快,增删慢 线程安全, 效率较低 LinkedList 底 ...
- BTSync 2.0 Vs. 1.4 Folders
Sync 2.0 supports boths new 2.0 folders and classic 1.4 folders, s o when you upgrade your Sync it w ...
- SQL 列转行,即多行合并成一条
需求:按照分组,将多条记录内容合并成一条,效果如下: 数据库示例: CREATE TABLE [t2]([NID] [bigint] NULL,[district] [nvarchar](255) N ...
- PHP个人博客系统开发历程
声明: Author:GenialX GenialX's QQ:2252065614 GenialX's URL:胡旭博客 - www.ihuxu.com 一年多曾经的某一天,我在上交实验报告时,偶然 ...
- IIS 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral,
在Windows Server 2008中的IIS服务器中部署WCF服务程序时,通过浏览器访问报出如下错误: 未能从程序集“System.ServiceModel, Version=3.0.0.0, ...