首页 > 代码库 > 邮件发送

邮件发送

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Net.Mail;  6 using System.Net.Mime;  7 using System.IO;  8 using System.Timers;  9 using System.Xml; 10  11 namespace MyEmailTest 12 { 13     class Program 14     { 15         static void Main(string[] args) 16         { 17             try 18             { 19                 string senderServerIp = "smtp.qq.com"; 20                 string toMailAddress = "mingmingruyuedlut@163.com"; 21                 string fromMailAddress = "mingmingruyuedlut@163.com"; 22                 string subjectInfo = "Test sending e_mail"; 23                 string bodyInfo = "Hello Eric, This is my first testing e_mail"; 24                 string mailUsername = "mingmingruyuedlut";  25                 string mailPassword = "..........";  26                 string mailPort = "25"; 27                 string attachPath = "E:\\123123.txt; E:\\haha.pdf"; 28  29                 MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false); 30                 email.AddAttachments(attachPath); 31                 email.Send(); 32             } 33             catch (Exception ex) 34             { 35                 Console.WriteLine(ex.ToString()); 36             } 37         } 38     } 39  40     public class MyEmail 41     { 42         private MailMessage mMailMessage;   //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等) 43         private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等) 44         private int mSenderPort;   //发送邮件所用的端口号(htmp协议默认为25) 45         private string mSenderServerHost;    //发件箱的邮件服务器地址(IP形式或字符串形式均可) 46         private string mSenderPassword;    //发件箱的密码 47         private string mSenderUsername;   //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello) 48         private bool mEnableSsl;    //是否对邮件内容进行socket层加密传输 49         private bool mEnablePwdAuthentication;  //是否对发件人邮箱进行密码验证 50  51         ///<summary> 52         /// 构造函数 53         ///</summary> 54         ///<param name="server">发件箱的邮件服务器地址</param> 55         ///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param> 56         ///<param name="fromMail">发件人地址</param> 57         ///<param name="subject">邮件标题</param> 58         ///<param name="emailBody">邮件内容(可以以html格式进行设计)</param> 59         ///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param> 60         ///<param name="password">发件人邮箱密码</param> 61         ///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param> 62         ///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param> 63         ///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param> 64         public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) 65         { 66             try 67             { 68                 mMailMessage = new MailMessage(); 69                 mMailMessage.To.Add(toMail); 70                 mMailMessage.From = new MailAddress(fromMail); 71                 mMailMessage.Subject = subject; 72                 mMailMessage.Body = emailBody; 73                 mMailMessage.IsBodyHtml = true; 74                 mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; 75                 mMailMessage.Priority = MailPriority.Normal; 76                 this.mSenderServerHost = server; 77                 this.mSenderUsername = username; 78                 this.mSenderPassword = password; 79                 this.mSenderPort = Convert.ToInt32(port); 80                 this.mEnableSsl = sslEnable; 81                 this.mEnablePwdAuthentication = pwdCheckEnable; 82             } 83             catch (Exception ex) 84             { 85                 Console.WriteLine(ex.ToString()); 86             } 87         } 88  89         ///<summary> 90         /// 添加附件 91         ///</summary> 92         ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param> 93         public void AddAttachments(string attachmentsPath) 94         { 95             try 96             { 97                 string[] path = attachmentsPath.Split(;); //以什么符号分隔可以自定义 98                 Attachment data; 99                 ContentDisposition disposition;100                 for (int i = 0; i < path.Length; i++)101                 {102                     data = http://www.mamicode.com/new Attachment(path[i], MediaTypeNames.Application.Octet);103                     disposition = data.ContentDisposition;104                     disposition.CreationDate = File.GetCreationTime(path[i]);105                     disposition.ModificationDate = File.GetLastWriteTime(path[i]);106                     disposition.ReadDate = File.GetLastAccessTime(path[i]);107                     mMailMessage.Attachments.Add(data);108                 }109             }110             catch (Exception ex)111             {112                 Console.WriteLine(ex.ToString());113             }114         }115 116         ///<summary>117         /// 邮件的发送118         ///</summary>119         public void Send()120         {121             try122             {123                 if (mMailMessage != null)124                 {125                     mSmtpClient = new SmtpClient();126                     //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;127                     mSmtpClient.Host = this.mSenderServerHost;128                     mSmtpClient.Port = this.mSenderPort;129                     mSmtpClient.UseDefaultCredentials = false;130                     mSmtpClient.EnableSsl = this.mEnableSsl;131                     if (this.mEnablePwdAuthentication)132                     {133                         System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);134                         //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);135                         //NTLM: Secure Password Authentication in Microsoft Outlook Express136                         mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");137                     }138                     else139                     {140                         mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);141                     }142                     mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;143                     mSmtpClient.Send(mMailMessage);144                 }145             }146             catch (Exception ex)147             {148                 Console.WriteLine(ex.ToString());149             }150         }151     }152 }

 

邮件发送