发送邮件的两种方式

一、实现代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sendMailContent = "我是用sendMail方式发送的";
        string CDOsendMailContent = "我是用CDOsendMail方式发送的";


        bool rr = true;
        bool rr1 = true;

        //使用企业邮箱发送邮件-------------------------------------------
        rr = SenMailBySMTP("[email protected]", "[email protected]", "表达式测试", sendMailContent, "mail.micromarketing.com.cn", "[email protected]", "******");
        rr1 = CDOsendMail("[email protected]", "表达式测试", "[email protected]", "[email protected]", "[email protected]", "******", "mail.micromarketing.com.cn", "[email protected]", CDOsendMailContent);
        //------------------------------------------------------------

        //使用hotmail及gmail邮箱发送邮件---------------------------------
        //rr = SenMailBySMTP("[email protected]", "[email protected]", "表达式测试", sendMailContent, "smtp.live.com", "[email protected]", "******");
        //rr1 = CDOsendMail("[email protected]", "表达式测试", "[email protected]", "[email protected]", "[email protected]", "******", "smtp.live.com", "[email protected]", CDOsendMailContent);
        //------------------------------------------------------------

        if (rr)
        {
            Response.Write("sendmailbystmp发送成功");
        }
        else
        {
            Response.Write("sendmailbystmp发送失败");
        }

        if (rr1)
        {
            Response.Write("CDOsendMail发送成功");
        }
        else
        {
            Response.Write("CDOsendMail发送失败");
        }
    }

    /// <summary>
    /// 使用CDO组件发送邮件CDO是Collaboration Data Objects的简称,它是一组高层的COM对象集合,并经历了好几个版本的演化,现在在Windows2000和Exchange2000中使用的都是CDO2.0的版本(分别为cdosys.dll和cdoex.dll)。CDOSYS构建在SMTP协议和NNTP协议之上,并且作为Windows2000 Server的组件被安装,您可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。CDO组件相对于先前介绍的SmtpMail对象功能更为丰富,并提供了一些SmtpMail类所没有提供的功能,如通过需要认证的SMTP服务器发送邮件等。
    /// </summary>
    /// <param name="from">from名称</param>
    /// <param name="subject">主题</param>
    /// <param name="senderEmail">发送者email</param>
    /// <param name="emailAccount">发送者账号</param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="smtp"></param>
    /// <param name="toEmail"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public bool CDOsendMail(string from, string subject, string senderEmail, string emailAccount, string username, string password, string smtp, string toEmail, string content)
    {
        try
        {
            CDO.Message oMsg = new CDO.Message();

            oMsg.From = "表达式邮件服务中心<" + from + ">";//from; 
            oMsg.To = toEmail;
            oMsg.Subject = subject;
            //oMsg.HTMLBody = "<html><body><b>大家好!!</b><img src=\"http://www.micromarketing.com.cn/jmail/count.aspx?id=505&email=<收件人地址>\" width=0 height=0>link</a></body></html>";
            oMsg.HTMLBody = content;

            CDO.IConfiguration iConfg = oMsg.Configuration;
            ADODB.Fields oFields = iConfg.Fields;

            oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;

            oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"]
                .Value = senderEmail; //sender mail

            oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"]
                .Value = emailAccount; //email account

            oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"]
                .Value = username;
            oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]
                .Value = password;

            oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]
                .Value = 1;

            //value=0 代表Anonymous验证方式(不需要验证)
            //value=1 代表Basic验证方式(使用basic (clear-text) authentication. 
            //The configuration sendusername/sendpassword or postusername/postpassword fields 
            //	are used to specify credentials.)
            //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)

            oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
            oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = smtp;

            //oFields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value = 60;//超时设置, 以秒为单位

            //hotmail、gmail邮箱要求启用以下两项
            //oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25;
            //oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = true;  //'是否使用套接字 true/false 


            oFields.Update();
            oMsg.BodyPart.Charset = "gb2312";
            oMsg.HTMLBodyPart.Charset = "gb2312";

            oMsg.Send();
            oMsg = null;


        }
        catch (Exception e)
        {
            Response.Write(e.Message);
            return false;

        }

        return true;

    }


    /// <summary>
    /// 通过smtp直接发送邮件
    /// </summary>
    /// <param name="fromEmail"></param>
    /// <param name="toEmail"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    /// <param name="smtp"></param>
    /// <param name="account"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    protected bool SenMailBySMTP(string fromEmail, string toEmail, string subject, string body, string smtpAddress, string account, string pwd)
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);

        //mail
        mail.From = new MailAddress(fromEmail, "表达式邮件服务中心", Encoding.UTF8);
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Subject = subject;
        mail.IsBodyHtml = true; //是否允许内容为 HTML 格式
        mail.BodyEncoding = Encoding.UTF8;
        mail.Body = body;

        //mail.Attachments.Add(new Attachment("E:\\foo.txt")); //添加一个附件

        SmtpClient smtp = new SmtpClient(smtpAddress);

        //hotmail、gmail邮箱要求启用以下两项
        //smtp.Port = 25; //smtp.Port = 587;//Gmail使用的端口 
        //smtp.EnableSsl = true; 


        smtp.Credentials = new NetworkCredential(account, pwd); //SMTP 验证
        //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

        bool rr = true;

        try
        {
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            rr = false;
        }

        mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定


        return rr;

    }
}


注:组件支持在附件中

二、可选项内容(参考)
附錄 A Web儲存系統結構描述屬性

你可能感兴趣的:(Web,UI,Microsoft,asp.net,Gmail)