zoukankan      html  css  js  c++  java
  • 使用 OpenSmtp.dll 发送邮件 (记录) 西安

    使用 OpenSmtp.dll 发送邮件关键代码
    1 /// <summary>
    2  /// 发送邮件XXXXXXXXXXX
    3  /// </summary>
    4  /// <param name="server">服务器</param>
    5  /// <param name="sender">寄件人</param>
    6  /// <param name="recipient">接收者</param>
    7 /// <param name="subject">主题(标题)</param>
    8 /// <param name="body">正文</param>
    9 /// <param name="isBodyHtml">bool 正文是否html格式 发送</param>
    10 /// <param name="encoding">字符编码</param>
    11 /// <param name="isAuthentication">bool 是否验证发件人</param>
    12 /// <param name="files">params string[] 应该是附件</param>
    13 public static void Send(string server, string sender, string recipient, string subject,
    14 string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
    15 {
    16 //根据邮件服务器smtp服务器创建一个对象
    17 SmtpClient smtpClient = new SmtpClient(server);
    18 MailMessage message = new MailMessage(sender, recipient);
    19 message.IsBodyHtml = isBodyHtml;
    20
    21 message.SubjectEncoding = encoding;
    22 message.BodyEncoding = encoding;
    23
    24 message.Subject = subject;
    25 message.Body = body;
    26
    27 message.Attachments.Clear();//清楚附件集合
    28 if (files != null && files.Length != 0)
    29 {
    30 for (int i = 0; i < files.Length; ++i)
    31 {
    32 Attachment attach = new Attachment(files[i]);
    33 message.Attachments.Add(attach);
    34 }
    35 }
    36 //如果指示 验证发件人身份
    37 if (isAuthentication == true)
    38 {
    39 smtpClient.Credentials = new NetworkCredential(
    40 SmtpConfig.Create().SmtpSetting.User,
    41 SmtpConfig.Create().SmtpSetting.Password);
    42 }
    43 //将指定的右键发送到SMTP服务器以便传递
    44 smtpClient.Send(message);
    45 }
    Smtp 配置文件 操作类
    1 /// <summary>
    2 /// Smtp 配置文件 操作类
    3 /// </summary>
    4 public class SmtpConfig
    5 {
    6 private static SmtpConfig _smtpConfig;
    7 private string ConfigFile
    8 {
    9 get
    10 {
    11 string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
    12 if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
    13 {
    14 configPath = HttpContext.Current.Request.MapPath("~/Config/SmtpSetting.config");
    15 }
    16 else
    17 {
    18 if (!Path.IsPathRooted(configPath))
    19 configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
    20 else
    21 configPath = Path.Combine(configPath, "SmtpSetting.config");
    22 }
    23 return configPath;
    24 }
    25 }
    26 /// <summary>
    27 /// public SmtpSetting SmtpSetting
    28 /// </summary>
    29 public SmtpSetting SmtpSetting
    30 {
    31 get
    32 {
    33 XmlDocument doc = new XmlDocument();
    34 doc.Load(this.ConfigFile);
    35 SmtpSetting smtpSetting = new SmtpSetting();
    36 smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
    37 smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
    38 smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
    39 smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
    40 smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
    41
    42 return smtpSetting;
    43 }
    44 }
    45 private SmtpConfig()
    46 {
    47
    48 }
    49 /// <summary>
    50 /// public static SmtpConfig Create()
    51 /// </summary>
    52 /// <returns></returns>
    53 public static SmtpConfig Create()
    54 {
    55 if (_smtpConfig == null)
    56 {
    57 _smtpConfig = new SmtpConfig();
    58 }
    59 return _smtpConfig;
    60 }
    61 }
    SmtpSetting.config配置
    1 <?xml version="1.0" encoding="utf-8"?>
    2 <configuration>
    3 <Server>smtp服务器地址</Server>
    4 <Authentication>true</Authentication>
    5 <User>发信邮箱地址</User>
    6 <Password>发信密码</Password>
    7 <Sender>发信者,应该也是发信邮箱地址</Sender>
    8 </configuration>
  • 相关阅读:
    牛客网分糖果
    【bzoj3717】[PA2014]Pakowanie 状压dp
    【bzoj1042】[HAOI2008]硬币购物 背包dp+容斥原理
    [POI2007]堆积木Klo
    【bzoj5018】[Snoi2017]英雄联盟 背包dp
    BZOJ 1492 [NOI2007]
    bzoj 2741 [FOTILE模拟赛] L
    bzoj 1486 最小圈
    计数
    cf 700
  • 原文地址:https://www.cnblogs.com/zhouzhaokun/p/1779152.html
Copyright © 2011-2022 走看看