邮件发送(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 ...
随机推荐
- [ 原创 ]学习笔记-Android 中关于Cursor类的介绍
此博文转载自:http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html 主讲Cursor的用法 使用过 SQLite 数据库的童 ...
- Codeforces.739E.Gosha is hunting(DP 带权二分)
题目链接 \(Description\) 有\(n\)只精灵,两种精灵球(高级和低级),每种球能捕捉到第\(i\)只精灵的概率已知.求用\(A\)个低级球和\(B\)个高级球能捕捉到精灵数的最大期望. ...
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
A. Little Pony and Expected Maximum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- IO流-复制多极文件夹(递归实现)
package com.io.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- Flex父子窗体相互调用
Flex父子窗体相互调用 1.设计思路 (1)子窗体调用父窗体的方法 (2)子窗体做了改动后,返回父窗体,父窗体调用子窗体函数 2.设计源代码 (1)父窗体 ParentWindow.mxml: &l ...
- Syncovery 是目前功能最为强大的实时自动备份工具
Syncovery Pro(原名叫做Super Flexible Synchronizer) 是目前功能最为强大的实时自动备份工具,连FTP.WebDAV等全部支持! 最近从V6开始改用比较好记.易懂 ...
- C# string byte[] Base64 常用互相转换
参考: http://www.cnblogs.com/zxx193/p/3605238.html?utm_source=tuicool http://www.cnblogs.com/freeliver ...
- powerdesigner反向SQLServer2008数据库生成物理数据模型
方法一:通过数据库脚本生成物理数据模型 具体步骤如下图所示:
- UITableView 让 cell 被选中的颜色底色快速消失,而不是一直停留在cell上
//单元格被选中 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ...
- Android实例剖析笔记(三)
摘要:点介绍Activity的生命周期,通过一个简单的实验来摸索状态转换的机制 Activity的生命周期 Activity类中有许多onXXX形式的函数可以重载,比如onCreate,onStart ...