博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 发送邮件、附件 分类: C# 2014-12-...
阅读量:4620 次
发布时间:2019-06-09

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

WinForm窗体代码如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;//要添加的引用如下using System.Net.Mail;using System.Net.Mime;using System.Timers;using System.IO;namespace NovelCS {    public partial class Form3 : Form {        public Form3() {            InitializeComponent();        }                   private void Form3_Load(object sender, EventArgs e) {            //一些常见的发送邮件服务器地址 一般都是 stmp.**.com   **=qq 或 126 或163 或 gmail 有例外的,具体问度娘            string senderServerIp = "smtp.qq.com";                       string toMailAddress = "1449740504@qq.com";            string fromMailAddress = "1449740504@qq.com";            string subjectInfo = "My First Email";            string bodyInfo =@"Hello Jackerson, \r\n                                        This is my first testing e_mail.But what all thess is from  an author named Eric Sun.                                        Thank you,Eric Sun.";            string mailUsername = "1449740504";            string mailPassword = "******"; //发送人邮箱的密码            string mailPort = "25";   //邮箱端口号            string attachPath = "D:\\jsj.txt; D:\\jsj2.txt";//附件本地地址            MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);            email.AddAttachments(attachPath);            email.Send();        }                   }    public class MyEmail {        private MailMessage me;//主要处理发送邮件的内容,例:收发件人地址、标题、主体、图片        private SmtpClient sc;   //主要处理用smtp发送邮件的配置信息,例:发送邮件服务器地址、发送端口号、验证方式        private int mSenderPort;                         //发送邮件所用的端口号(htmp协议默认为25)        private string mSenderServerHost;          //发件箱的邮件服务器地址(IP形式或字符串形式均可)        private string mSenderUserName;           //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)        private string mSenderPassword;             //发件箱的密码        private bool mEnableSsl;                         //是否对邮件内容进行socket层加密传输        private bool mEnablePwdAuthentication;  //是否对发件人邮箱进行密码验证        ///        /// 构造函数        ///        ///发件箱的邮件服务器地址        ///收件人地址(可以是多个收件人,程序中是以“;"进行区分的)        ///发件人地址        ///邮件标题        ///邮件内容(可以以html格式进行设计)        ///发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)        ///发件人邮箱密码        ///发送邮件所用的端口号(htmp协议默认为25)        ///true表示对邮件内容进行socket层加密传输,false表示不加密        ///true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证        public MyEmail(string server, string toMailAddress, string fromMailAddress, string emailTitle, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) {            try {                me = new MailMessage();                me.To.Add(toMailAddress);                me.From = new MailAddress(fromMailAddress);                me.Subject = emailTitle;                me.Body = emailBody;                me.IsBodyHtml = true;                me.BodyEncoding = System.Text.Encoding.UTF8;                me.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) {                MessageBox.Show(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]);                    me.Attachments.Add(data);                }            } catch (Exception ex) {                MessageBox.Show(ex.ToString());            }        }        ///        /// 邮件的发送        ///        public void Send() {            try {                if (me != null) {                    sc = new SmtpClient();                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;                    sc.Host = this.mSenderServerHost;                    sc.Port = this.mSenderPort;                    sc.UseDefaultCredentials = false;                    sc.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                        sc.Credentials = nc.GetCredential(sc.Host, sc.Port, "NTLM");                    } else {                        sc.Credentials = new System.Net.NetworkCredential(this.mSenderUserName, this.mSenderPassword);                    }                    sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;                    sc.Send(me);                }            } catch (Exception ex) {                Console.WriteLine(ex.ToString());            }        }    }            }

备注:

(1)经过测试,确实可以使用。

(2)本文出处:Eric Sun()

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/Jackerson/p/4631985.html

你可能感兴趣的文章
plsql程序中循环语句的使用
查看>>
毛巾行业生产管理软件系统 淮安七夕软件有限公司
查看>>
Linux命令学习一日一命令(TOUCH)
查看>>
排序算法3---直接插入排序算法
查看>>
用matalb、python画聚类结果图
查看>>
JDK提供的几个基本注解
查看>>
Firebird数据库值得信赖吗?为什么我要在开发中选择...
查看>>
__rept__和__str__
查看>>
docker镜像基本操作
查看>>
算法第五章上机实践报告
查看>>
SHELL学习笔记----IF条件判断,判断条件
查看>>
RegisterWaitForSingleObject的使用
查看>>
UML第三次作业
查看>>
000 Python教程
查看>>
2013能量篇终止,2014精致篇起航
查看>>
力扣——分数排名(数据库的题
查看>>
力扣——行程与用户(数据库的题
查看>>
3.java基础语法(下)
查看>>
ios 11 系统CPU过高,xib中textfield使用导致出过高
查看>>
JS应用(资料很全)
查看>>