MailMessageを使用してメールを送信

7676 ワード

SmtpClient smtp = new SmtpClient(); //     SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // smtp        Network
            smtp.EnableSsl = false;//smtp       SSL  
            smtp.Host = "smtp.163.com"; //   smtp      
            smtp.Port = 25;             //   smtp       ,   25,        ,   
            //    SMTP          ,        ,  ,             
            smtp.UseDefaultCredentials = true;
            //      ,       
            smtp.Credentials = new NetworkCredential("[email protected]", "****");
            MailMessage mm = new MailMessage(); //        
            mm.Priority = MailPriority.High; //      ,   Low, Normal, High,    Normal  
            mm.From = new MailAddress("[email protected]", "      ", Encoding.GetEncoding(936));
            //          ;
            //             
            //             
            //                  ,       ,          
            //936      codepage 
            // :       ,              ,       
            mm.ReplyTo = new MailAddress("[email protected]", "      ", Encoding.GetEncoding(936));
            //ReplyTo                 , :        ,         
            //          ,   From    
            //mm.CC.Add("[email protected],[email protected],[email protected]");
            //      ,    ,                 
 
            //         ,  :
            mm.CC.Add(new MailAddress("[email protected]", "   A", Encoding.GetEncoding(936)));
            //mm.CC.Add(new MailAddress("[email protected]", "   B", Encoding.GetEncoding(936)));
            //mm.CC.Add(new MailAddress("[email protected]", "   C", Encoding.GetEncoding(936)));
 
            //mm.Bcc.Add("[email protected],[email protected]");
            //      ,    ,                 
 
            //         ,  :
            //mm.CC.Add(new MailAddress("[email protected]", "   D", Encoding.GetEncoding(936)));
            //mm.CC.Add(new MailAddress("[email protected]", "   E", Encoding.GetEncoding(936)));
            //mm.Sender = new MailAddress("[email protected]", "     ", Encoding.GetEncoding(936));
            //      ,          ,         ,         
            //   ,          ,      ,      
            //mm.To.Add("[email protected],[email protected]");
            //      ,    ,               
 
            //           
 
            mm.To.Add(new MailAddress("[email protected]", "   g", Encoding.GetEncoding(936)));
            //mm.To.Add(new MailAddress("[email protected]", "   h", Encoding.GetEncoding(936)));
            mm.Subject = "      -  "; //    
            mm.SubjectEncoding = Encoding.GetEncoding(936);
            //       ,            ,       ,              。
            // 936      pagecode,       ,        
            mm.IsBodyHtml = true; //       HTML  
 
            mm.BodyEncoding = Encoding.GetEncoding(936);
            //       ,      ,         
 
            mm.Body = "<font color=\"red\">    ,  </font>";
            //    
            mm.Attachments.Add(new Attachment(@"c:\d\1.doc", System.Net.Mime.MediaTypeNames.Application.Rtf));
            //    ,     ,         ,      
            //        
            //mm.Attachments.Add(new Attachment(@"d:b.doc"));
            smtp.Send(mm); //    ,       ,       。
 
 
        }

二、
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
/// <summary>
///      
/// Author:tonyepaper.cnblogs.com
/// </summary>
public class Mail
{
    private string senderAddress;
    /// <summary>
    ///    
    /// </summary>
    public string SenderAddress
    {
        get { return senderAddress; }
        set { senderAddress = value; }
    }
    private string receiverAddess;
    /// <summary>
    ///    
    /// </summary>
    public string ReceiverAddess
    {
        get { return receiverAddess; }
        set { receiverAddess = value; }
    }
    private string subject;
    /// <summary>
    ///   
    /// </summary>
    public string Subject
    {
        get { return subject; }
        set { subject = value; }
    }
    private string body;
    /// <summary>
    ///   
    /// </summary>
    public string Body
    {
        get { return body; }
        set { body = value; }
    }
    private string smtpHost;
    /// <summary>
    /// SMTP  
    /// </summary>
    public string SmtpHost
    {
        get { return smtpHost; }
        set { smtpHost = value; }
    }
    private int smtpPort;
    /// <summary>
    /// SMTP  
    /// </summary>
    public int SmtpPort
    {
        get { return smtpPort; }
        set { smtpPort = value; }
    }
    private string smtpPassword;
    /// <summary>
    /// SMTP  
    /// </summary>
    public string Password
    {
        get { return smtpPassword; }
        set { this.smtpPassword = value; }
    }
    /// <summary>
    ///       
    /// </summary>
    public Mail()
    {
        senderAddress = "[email protected]";
        smtpPassword ="*****";
        smtpHost = "smtp.gmail.com";
        smtpPort = 587;
    }
    /// <summary>
    ///   
    /// </summary>
    /// <param name="receiverAddess">     </param>
    /// <param name="subject">  </param>
    /// <param name="body">  </param>
    public Mail(string receiverAddess, string subject, string body)
        : this()
    {
        this.receiverAddess = receiverAddess;
        this.subject = subject;
        this.body = body;
    }
    /// <summary>
    ///     
    /// </summary>
    public bool Send()
    {
        MailMessage mailMessage = new MailMessage(senderAddress, receiverAddess);
        mailMessage.Subject = subject;
        mailMessage.Body = body;

        SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort);
        //  SSL    
        smtpClient.EnableSsl = true;
        NetworkCredential networkCredential = new NetworkCredential(senderAddress, smtpPassword);
        smtpClient.Credentials = networkCredential;
        try
        {
            smtpClient.Send(mailMessage);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }   

//  
protected void Button1_Click(object sender, EventArgs e)
    {
        Mail mail = new Mail("[email protected]", "aaaa", "aaaaaa");//      
        mail.Send();
            }