asp.net发送邮件类

来源:互联网 发布:x264 linux 编辑:程序博客网 时间:2024/06/07 01:22

结合网络上一些邮件发送的功能,特整理了这篇类库。懒羊

///名称:邮件发送类
///开发时间:2008-01-18
///开发人:杨剑(anchor)
///http://www.swordyang.cn
///sn1985boy@sohu.com


using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace cn.SwordYang
{
   
public class MailSender
    
{
protected int emailPriority=1;     //邮件紧急程度
protected string mailFormat="text";  //邮件类型
protected string fromMail;          //发件人邮箱地址
protected string toMail;            //收件人地址
protected string subject;           //邮件主题
protected string body;              //邮件内容
protected Encoding mailEncoding=Encoding.GetEncoding(936);      //邮件编码
//protected int isLocalSmtp=2;     //是否启用本地smtp服务器
protected string smtpServer;
protected int smtpPort=25;
//protected int isSmtpAuthenticate=1;   //是否需要验证
protected bool isSSL=true;  //是否需要SSL认证
protected string mailUserName;  //用户名
protected string mailPassWord;  //密码


属性


public string SendEmail()
{

    
try
    
{
        MailMessage msg 
= new MailMessage(fromMail,toMail);

        msg.Subject 
= subject;
        msg.Body 
= body;
        msg.BodyEncoding 
=mailEncoding;
        
          
//是否启用html
         switch (mailFormat)
        
{
            
case "html":
                msg.IsBodyHtml
=true;
                
break;
            
case "text":
                msg.IsBodyHtml
=false;
                
break;
        }


         
switch (emailPriority)
         
{
             
case 2:
                 msg.Priority 
= MailPriority.High;
                 
break;
             
case 1:
                 msg.Priority 
= MailPriority.Normal;
                 
break;
             
case 0:
                 msg.Priority 
= MailPriority.Low;
                 
break;
         }


         SmtpClient SC
=new SmtpClient(smtpServer,smtpPort);
         
if (!String.IsNullOrEmpty(mailUserName) && !String.IsNullOrEmpty(mailPassWord))
         
{
            SC.Credentials 
= new NetworkCredential(mailUserName, mailPassWord);
         }


         SC.EnableSsl
=isSSL;
SC.Send(msg);
return "Send Mail OK";

/*  原system.web.mail
        switch (mailPriority)
        {
            case 2:
                msg.Priority = MailPriority.High;
                break;
            case 1:
                msg.Priority = MailPriority.Normal;
                break;
            case 0:
                msg.Priority = MailPriority.Low;
                break;
        }

        switch (mailFormat)
        {
            case "html":
             msg.BodyFormat = MailFormat.Html;
                break;
            case "text":
                msg.BodyFormat = MailFormat.Text;
                break;
        }

        if (isLocalSmtp==2)
        {

            //1 代表使用 local smtp, 2 外部 smtp
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusing", isLocalSmtp);
            SmtpMail.SmtpServer = smtpServer;

            //SMTP Server
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);

            if (SmtpPort != "")
            {
                //Server port, gmail is 465
                msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpserverport", SmtpPort);
            }

            //Authentication method, ssl or not, Username and password for the SMTP Server
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",isSmtpAuthenticate);

            //cdoBasic 基本验证 gmail使用ssl验证
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpusessl", isSSL);


            //账号
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername", mailUserName);
            //密码
            msg.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassWord);
        }

        SmtpMail.Send(msg);

*/




    }

    
catch (Exception ex)
    
{
        
return ex.Message;
    }


}



    }

}

原创粉丝点击