博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#实现邮件发送的功能
阅读量:5875 次
发布时间:2019-06-19

本文共 5770 字,大约阅读时间需要 19 分钟。

 

 

Ø  发送邮件所用的核心知识点

 

  •   微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
  •   微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
  •   SmtpClient主要进行了三层的封装:Socket --> TcpClient --> SmtpClient

Ø  具体代码请看如下所示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net.Mime; using System.IO; using System.Timers; using System.Xml; namespace MyEmailTest {
class Program {
static void Main(string[] args) {
try {
//smtp.163.com string senderServerIp = "123.125.50.133"; //smtp.gmail.com //string senderServerIp = "74.125.127.109"; //smtp.qq.com //string senderServerIp = "58.251.149.147"; //string senderServerIp = "smtp.sina.com"; string toMailAddress = "mingmingruyuedlut@163.com"; string fromMailAddress = "mingmingruyuedlut@163.com"; string subjectInfo = "Test sending e_mail"; string bodyInfo = "Hello Eric, This is my first testing e_mail"; string mailUsername = "mingmingruyuedlut"; string mailPassword = ".........."; //发送邮箱的密码() string mailPort = "25"; string attachPath = "E:\\123123.txt; E:\\haha.pdf"; MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false); email.AddAttachments(attachPath); email.Send(); } catch (Exception ex) {
Console.WriteLine(ex.ToString()); } } } public class MyEmail {
private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等) private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等) private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25) private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可) private string mSenderPassword; //发件箱的密码 private string mSenderUsername; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello) private bool mEnableSsl; //是否对邮件内容进行socket层加密传输 private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 /// /// 构造函数 /// ///发件箱的邮件服务器地址 ///收件人地址(可以是多个收件人,程序中是以“;"进行区分的) ///发件人地址 ///邮件标题 ///邮件内容(可以以html格式进行设计) ///发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello) ///发件人邮箱密码 ///发送邮件所用的端口号(htmp协议默认为25) ///true表示对邮件内容进行socket层加密传输,false表示不加密 ///true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) {
try {
mMailMessage = new MailMessage(); mMailMessage.To.Add(toMail); mMailMessage.From = new MailAddress(fromMail); mMailMessage.Subject = subject; mMailMessage.Body = emailBody; mMailMessage.IsBodyHtml = true; mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; mMailMessage.Priority = MailPriority.Normal; this.mSenderServerHost = server; this.mSenderUsername = username; this.mSenderPassword = password; this.mSenderPort = Convert.ToInt32(port); this.mEnableSsl = sslEnable; this.mEnablePwdAuthentication = pwdCheckEnable; } catch (Exception ex) {
Console.WriteLine(ex.ToString()); } } /// /// 添加附件 /// ///附件的路径集合,以分号分隔 public void AddAttachments(string attachmentsPath) {
try {
string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义 Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) {
data = new Attachment(path[i], MediaTypeNames.Application.Octet); disposition = data.ContentDisposition; disposition.CreationDate = File.GetCreationTime(path[i]); disposition.ModificationDate = File.GetLastWriteTime(path[i]); disposition.ReadDate = File.GetLastAccessTime(path[i]); mMailMessage.Attachments.Add(data); } } catch (Exception ex) {
Console.WriteLine(ex.ToString()); } } /// /// 邮件的发送 /// public void Send() {
try {
if (mMailMessage != null) {
mSmtpClient = new SmtpClient(); //mSmtpClient.Host = "smtp." + mMailMessage.From.Host; mSmtpClient.Host = this.mSenderServerHost; mSmtpClient.Port = this.mSenderPort; mSmtpClient.UseDefaultCredentials = false; mSmtpClient.EnableSsl = this.mEnableSsl; if (this.mEnablePwdAuthentication) {
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); //NTLM: Secure Password Authentication in Microsoft Outlook Express mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM"); } else {
mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); } mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; mSmtpClient.Send(mMailMessage); } } catch (Exception ex) {
Console.WriteLine(ex.ToString()); } } } }

。。。。。。。。。

 

 

 

 

 

 

转载地址:http://gqzix.baihongyu.com/

你可能感兴趣的文章
[Spark][Python]Spark Join 小例子
查看>>
form表单下的button按钮会自动提交表单的问题
查看>>
大战设计模式【11】—— 模板方法模式
查看>>
springBoot介绍
查看>>
Intellij IDEA 快捷键整理
查看>>
Redis 通用操作2
查看>>
性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项
查看>>
11. Spring Boot JPA 连接数据库
查看>>
洛谷P2925 [USACO08DEC]干草出售Hay For Sale
查看>>
MapReduce工作原理流程简介
查看>>
那些年追过的......写过的技术博客
查看>>
小米手机解锁bootload教程及常见问题
查看>>
Python内置函数property()使用实例
查看>>
Spring MVC NoClassDefFoundError 问题的解决方法。
查看>>
CentOS 6.9配置网卡IP/网关/DNS命令详细介绍及一些常用网络配置命令(转)
查看>>
python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
查看>>
C# 解决窗体闪烁
查看>>
CSS魔法堂:Transition就这么好玩
查看>>
【OpenStack】network相关知识学习
查看>>
centos 7下独立的python 2.7环境安装
查看>>