熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

郵件發送組件

2022-06-13   來源: .NET編程 

  先添加引入命名空間 SystemNetMail

  組件代碼:

  /// <summary>

  /// 郵件發送組件

  /// </summary>

  public class EmailUtils

  {

  static EmailUtils() {

  MailHost = ConfigurationManagerAppSettings[mailHost];

  MailUserName = ConfigurationManagerAppSettings[mailUserName];

  MailPsw = ConfigurationManagerAppSettings[mailPsw];

  MailFrom = ConfigurationManagerAppSettings[mailFrom];

  }

  public static string MailFrom {

  get;

  set;

  }

  public static string MailHost {

  get;

  set;

  }

  public static string MailUserName {

  get;

  set;

  }

  public static string MailPsw {

  get;

  set;

  }

  /// <summary>

  /// 發送郵件

  /// </summary>

  /// <param name=mailto>發送到</param>

  /// <param name=title>標題</param>

  /// <param name=msg></param>

  /// <returns></returns>

  public static bool Send(string mailto string title string msg) {

  var client = new SmtpClient();

  clientHost = MailHost;

  clientUseDefaultCredentials = false;

  clientCredentials = new SystemNetNetworkCredential(MailUserName MailPsw);

  clientDeliveryMethod = SmtpDeliveryMethodNetwork;

  SystemNetMailMailMessage message = new MailMessage(MailFrom mailto);

  messageSubject = title;

  messageBody = msg;

  messageBodyEncoding = SystemTextEncodingUTF;

  messageIsBodyHtml = true;

  try {

  clientSend(message);

  return true;

  }

  catch {

  return false;

  }

  }

  }

  使用時直接調用 Send 方法即可


From:http://tw.wingwit.com/Article/program/net/201311/12764.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.