zoukankan      html  css  js  c++  java
  • 【C#】Smtp发送邮件

     1 class SmtpEmail
     2     {
     3         SmtpClient smtpclient;
     4         MailMessage msg;
     5         Attachment attachment;
     6         public void sendMail(String form, String[] to, String subject, String body, String attachmentpath)
     7         {
     8             if (to == null || to.Length <= 0) {
     9                 return;
    10             }
    11             try
    12             {
    13                 smtpclient = new SmtpClient("smtp.URL");
    14                 smtpclient.UseDefaultCredentials = true;
    15                 smtpclient.Credentials = new NetworkCredential("username", "*****");
    16                 smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
    17                 msg = new MailMessage();
    18                 
    19 
    20                 for (int i = 0; i < to.Length; i++)
    21                 {
    22                     msg.To.Add(to[i]);
    23                 }
    24                 if (msg.To.Count <= 0) {
    25                     return;
    26                 }
    27                 msg.From = form;
    28                 msg.Subject = subject;
    29                 msg.Body = body;
    30                 if (attachment!=null && !attachmentpath.Length.Equals(0))
    31                 {
    32                     this.attachment = new Attachment(attachmentpath);
    33                     msg.Attachments.Add(attachment);
    34                 }
    35                 msg.BodyEncoding = Encoding.UTF8;
    36                 msg.IsBodyHtml = true;
    37                 smtpclient.Send(msg);
    38             }
    39             catch (Exception err)
    40             {
    41                 MessageBox.Show(err.Message);
    42                 return;
    43             }
    44         }
    45     }
  • 相关阅读:
    wampserver服务器离线,无法访问此网站 找不到 项目 的服务器 DNS 地址。
    node.js(一)介绍与安装
    js全局函數
    类和对象的定义
    iframe框架学习
    while和do-while的区别
    html5视频音频
    列表
    html表格的学习
    云课堂数组1
  • 原文地址:https://www.cnblogs.com/yomho/p/3272062.html
Copyright © 2011-2022 走看看