程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#發送eMail的類

C#發送eMail的類

編輯:關於C語言
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mail;
namespace BaseLib
{
  public class SendMail
  {
    public void sendTxtMail(string from, string pass, string to, string subject, MailPriority priority, string body, string smtpServer, System.Collections.ArrayList files)
    {
      MailMessage msg = new MailMessage();
      msg.From = from;
      msg.To = to;
      msg.Subject = subject;
      msg.Priority = priority;
      msg.BodyFormat = MailFormat.Text;
      msg.Body = body;
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from.Substring(0, from.IndexOf("@"))); //set your username here
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); //set your passWord here
      for (int i = 0; i < files.Count; i++)
      {
        if (System.IO.File.Exists(files[i].ToString()))
        {
          msg.Attachments.Add(new MailAttachment(files[i].ToString()));
        }
      }
      SmtpMail.SmtpServer = smtpServer;
      try
      {
        SmtpMail.Send(msg);
      }
      catch(Exception ex)
      {
      }
    }
    public void sendHtmlMail(string from, string pass, string to, string subject, MailPriority priority, string body, string smtpServer, System.Collections.ArrayList files)
    {
      MailMessage msg = new MailMessage();
      msg.From = from;
      msg.To = to;
      msg.Subject = subject;
      msg.Priority = priority;
      msg.BodyFormat = MailFormat.Html;
      msg.Body = body;
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from.Substring(0, from.IndexOf("@"))); //set your username here
      msg.FIElds.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); //set your passWord here
      for (int i = 0; i < files.Count; i++)
      {
        if (System.IO.File.Exists(files[i].ToString()))
        {
          msg.Attachments.Add(new MailAttachment(files[i].ToString()));
        }
      }
      SmtpMail.SmtpServer = smtpServer;
      SmtpMail.Send(msg);
    }
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved