メールを送る2つの方法


一、実現コードは以下の通りである:

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ストレージシステム構造説明属性