邮件发送(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 ... 
随机推荐
- [leetcode greedy]45. Jump Game II
			Given an array of non-negative integers, you are initially positioned at the first index of the arra ... 
- gpfs中遇到的错误
			主要导致这个问题是之前GPFS格式化的磁盘会留下gpfs的一些信息 mmcrnsd: Disk name nsd01 is already registered for use by GPFS.mmc ... 
- 制作Linux内核
			<linux内核简介> <linux系统架构> 系统架构 用户部分: 应用程序:GNU C 库内核部分:系统调用接口.内核.体系结构相关代码(与硬件相关的代码) 划分原因:不同 ... 
- luoguP4466 [国际集训队]和与积 莫比乌斯反演
			自然想到枚举\(gcd(a, b)\),不妨设其为\(d\),并且\(a = di, b = dj(a > b)\) 那么\(\frac{ab}{a + b} = \frac{dij}{i + ... 
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
			https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ... 
- LOJ P3959 宝藏 状压dp noip
			https://www.luogu.org/problemnew/show/P3959 考场上我怎么想不出来这么写的,状压白学了. 直接按层次存因为如果某个点在前面存过了则肯定结果更优所以不用在意各点 ... 
- BZOJ.2460.[BeiJing2011]元素(线性基 贪心)
			题目链接 线性基:https://blog.csdn.net/qq_36056315/article/details/79819714. \(Description\) 求一组矿石,满足其下标异或和不 ... 
- Swift教程之枚举语法
			import Foundation //MARK:-------枚举语法----------- //不像 C 和 Objective-C 一样.Swift 的枚举成员在被创建时不会被赋予一个默认的整数 ... 
- windows设置共享
			设置共享: 添加用户 点击添加 设置权限 然后别人就可以查看了. 查看共享: 删除共享: 
- Bootstrap 3之美03-独立行,文字环绕,图片自适应,隐藏元素
			本篇主要包括: ■ 添加独立的一行■ 文字环绕■ 图片自适应■ 隐藏元素 添加独立的一行 在id为body的section和id为main的section之间,添加2张图片. 我们发现,新加的 ... 
