zoukankan      html  css  js  c++  java
  • 邮件客户端开发

    .Net自带封装的Mail接口(System.Net.Mail)

    方法实现

     1  static readonly string cc = ConfigurationManager.AppSettings["cc"];
     2         //app.config使用如下注释的代买读取配置,web.config使用6行代码读取配置
    3 //static Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 4 //static SmtpSection smtp = NetSectionGroup.GetSectionGroup(config).MailSettings.Smtp; 5 6 static readonly SmtpSection smtp = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp; 7 8 9 /// <summary> 10 /// 发送邮件 11 /// </summary> 12 /// <param name="message">邮件内容</param> 13 public static void PublishTo(MailEntity message) 14 { 15 try 16 { 17 string userAddress = message.ToAddress; 18 string userName = message.ToName; 19 string subject = message.Subject; 20 string body = message.Body; 21 Encoding encoding = Encoding.UTF8; 22 bool isBodyHtml = true; 23 var fromAddress = new MailAddress(smtp.Network.UserName, message.FromName); //含有发送人的发送地址 24 var toAddress = new MailAddress(userAddress, userName); //含有收件人的接收地址 25 var email = new MailMessage(fromAddress, toAddress) 26 { 27 IsBodyHtml = isBodyHtml, 28 Subject = subject, 29 Body = body, 30 BodyEncoding = encoding, 31 }; 32 string copys = cc; 33 if (!string.IsNullOrEmpty(message.CC)) 34 { 35 //如果传入cc则不读配置cc 36 copys = message.CC; 37 } 38 if (!string.IsNullOrEmpty(copys)) 39 { 40 if (copys.IndexOf(';') == -1) 41 { 42 email.CC.Add(copys); 43 } 44 else 45 { 46 string[] ccs = cc.Split(';'); 47 foreach (var s in ccs) 48 { 49 MailAddress copy = new MailAddress(s); 50 email.CC.Add(copy); 51 } 52 } 53 } 54 //发送附件 55 if (!string.IsNullOrEmpty(message.Attachment) && !string.IsNullOrEmpty(message.AttachmentName)) 56 { 57 Stream fileStream = File.OpenRead(message.Attachment); 58 ContentType ct = new ContentType(MediaTypeNames.Text.Plain); 59 Attachment attachment = new Attachment(fileStream, ct); 60 ContentDisposition disposition = attachment.ContentDisposition; 61 //文件名不支持中文。如果是disposition.FileName为中文名则发生失败 62 disposition.FileName = message.AttachmentName; 63 email.Attachments.Clear(); 64 email.Attachments.Add(attachment); 65 } 66 //邮件附件以流的方式写入 67 //email.Attachments.Add(new Attachment());//不支持uri 68 69 PublicTo(email); 70
    71
    } 72 catch (Exception e) 73 { 74 //增加日记 75 } 76 } 77 78 static void PublicTo(MailMessage mail) 79 { 80 var client = new SmtpClient 81 { 82 Host = smtp.Network.Host, 83 Credentials = new NetworkCredential(smtp.Network.UserName, smtp.Network.Password), 84 Port = smtp.Network.Port, 85 }; 86 87 //开始发送 88 client.Send(mail); 89 client.Dispose(); 90 }

    配置文件

    1     <mailSettings>
    2       <smtp deliveryMethod="Network">
    3         <network host="mail.5173.com" port="25" userName="archProjectMain@5173.com" password="eue72dsh" />
    4       </smtp>
    5     </mailSettings>

    邮件实体

        /// <summary>
        /// 邮件实体
        /// </summary>
        [Serializable]
        public class MailEntity
        {
            /// <summary>
            /// 邮件唯一标识,用于备份数据库,便于查找
            /// </summary>
            public string MailId
            {
                get { return DateTime.Now.ToString("yyyyMMdd-HHmmss"); }
            }
    
            /// <summary>
            /// 发送人名
            /// </summary>
            public string FromName { get; set; }
    
            /// <summary>
            /// 收件人名
            /// </summary>
            public string ToName { get; set; }
    
            /// <summary>
            /// 收件人地址
            /// </summary>
            public string ToAddress { get; set; }
    
            /// <summary>
            /// 标题
            /// </summary>
            public string Subject { get; set; }
    
            /// <summary>
            /// 内容
            /// </summary>
            [MongoIgnore]
            public string Body { get; set; }
    
            /// <summary>
            /// 抄送 抄送人以;隔离 
    /// </summary> public string CC { get; set; } /// <summary> /// 邮件附件 /// </summary> public string Attachment { get; set; } /// <summary> /// 邮件附件名称,不能有中文,否则发送会失败 /// </summary> public string AttachmentName { get; set; } }

      

    人总要去积累生活、工作上的点点滴滴,慢慢的进步,以后回头看看,笑笑,顺便学学,人都说回忆才是最美的。
  • 相关阅读:
    [恢]hdu 1406
    [恢]hdu 1870
    [恢]hdu 1877
    [恢]hdu 1018
    [转载]Delphi 的编译指令(2): 条件语句的更多用法
    关于 class helper for ... 语法
    [转载]Delphi 的编译指令(1): $DEFINE、$UNDEF、$IFDEF等
    [转载]Delphi 的编译指令(3): 常用的预定义条件标识符
    Delphi2009之TStringBuilder类[3]:Replace
    Delphi2009之TStringBuilder类[1]:Create
  • 原文地址:https://www.cnblogs.com/jueye/p/2984239.html
Copyright © 2011-2022 走看看