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#)的更多相关文章

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

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

  2. J2EE 邮件发送那些事儿

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

  3. 结合ABP源码实现邮件发送功能

    1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...

  4. SSH项目里面 忘记密码的邮件发送功能

    package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...

  5. [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作

    这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...

  6. java spring 邮件发送

    开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...

  7. Java邮件发送与接收原理

    一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...

  8. c#实现邮件发送链接激活

    2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...

  9. .Net(C#)最简单的邮件发送案例

    一.序言 刚开始接触邮件发送功能的时候,在网上找的资料都挺复杂的!对于新手入门有点难(至少对于本人来说,第一次接触的时候确实不容易).这里就写一段简单的邮箱发送代码,备忘,也给新手一个参考(相关类的字 ...

  10. SpringMVC 邮件发送

    <!--邮件发送实现类--> <bean id="javaMailSender" class="org.springframework.mail.jav ...

随机推荐

  1. python 全排列

    itertools模块现成的全排列: for i in itertools.permutations('abcd',4): print ''.join(i) 相关全排列算法: def perm(l): ...

  2. pkuwc2019自闭记

    窝自闭了... 所以这篇\(blog\)咕咕咕了.

  3. Gunicorn配置部分的翻译

    写在前面,虽然翻译得很烂,但也是我的劳动成果,转载请注明出处,谢谢. Gunicorn版本号19.7.1 Gunicorn配置 概述 三种配置方式 优先级如下,越后的优先级越大 1.框架的设置(现在只 ...

  4. PIXIV 爬取国际前100名代码

    PYTHON爬虫 爬取PIXIV国际前100名的代码 代码是别人的,那天学习爬虫的时候看到了,写的很厉害~ 学习学习 #coding:UTF-8 __author__ = 'monburan' __v ...

  5. javaresource 红色X

    出现这个问题的原因很多,解决办法也很多,我这里只记录我所遇到的. 这个问题一直存在,但是不影响项目运行. 后来在网上找了下资料,有一篇文章是让修改maven的settings.xml.将jdk1.6修 ...

  6. Maven的POM文件parent节点不可以使用properties里面的变量

    Maven的POM文件parent节点不可以使用properties里面的变量 但是如果在子项目上的parent节点是可以使用父项目里定义的properties变量 如果一开始为单项目,或者最顶层项目 ...

  7. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  8. 【我所认知的BIOS】—&gt; uEFI AHCI Driver(5) — 第一个protocol最终要開始安装了

    [我所认知的BIOS]-> uEFI AHCI Driver(5) - 第一个protocol最终要開始安装了 LightSeed 4/28/2014 文章对EFI_DRIVER_BINDING ...

  9. java基础学习总结——面向对象2

    一.JAVA类的定义

  10. mormot当作内存数据库(缓存)使用

    mormot当作内存数据库(缓存)使用 mormot的TSQLRestStorageInMemory可以作为内存数据库来使用. 上图是在笔者4代I5笔记本上做的测试,增加10万记录,耗时:562毫秒. ...