VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#发送Email的类

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

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);
    }
  }
}



相关教程