zoukankan      html  css  js  c++  java
  • C#、ASP.NET、WinForm

    转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html

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

    要开启邮箱的SmTP服务。

  • 相关阅读:
    微软经典面试笔试题
    Websense一面、二面及Offer
    微软2014年技术岗位在线笔试题
    2013年微软面试经历 – 终究离他们的要求还是有一定距离
    Mesos:数据库使用的持久化卷
    Mesos和Docker的集成
    可扩展架构取舍
    组织架构适配下的敏捷开发
    TensorFlow与主流深度学习框架对比
    Pokémon Go呼应设计:让全世界玩家疯狂沉迷
  • 原文地址:https://www.cnblogs.com/KTblog/p/4657042.html
Copyright © 2011-2022 走看看