Asp.Net 2.0 郵件發送

 1using System;
 2using System.Text;
 3using System.Net.Mail;
 4using System.Configuration;
 5
 6/// <summary>
 7/// Email 的摘要说明
 8/// </summary>

 9public class Email
10{
11    /// <summary>
12    /// 郵件發送函數(靜態)
13    /// </summary>
14    /// <param name="mailFromAddress">發送人郵件地址</param>
15    /// <param name="mailFromName">發送人名稱</param>
16    /// <param name="mailTo">收件人</param>
17    /// <param name="mailCc">抄送人</param>
18    /// <param name="mailSubject">主題</param>
19    /// <param name="mailBody">郵件內容</param>

20    public static void SendMail(string mailFromAddress, string mailFromName, string mailTo, string mailCc, string mailSubject, string mailBody)
21    {
22        //create the mail message
23        MailMessage mail = new MailMessage();
24
25        //set the addresses
26        mail.From = new MailAddress(mailFromAddress, mailFromName);
27
28        mail.To.Add(mailTo);
29        mail.CC.Add(mailCc); ;
30        //mail.Bcc.Add("sfwu@cclmotors.com");
31
32        //set the content
33        mail.Subject = mailSubject;
34        mail.Body = mailBody;
35        //add an attachment from the filesystem
36        //mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));
37
38        mail.IsBodyHtml = true;
39
40        //specify the priority of the mail message
41        //mail.Priority = MailPriority.High;
42
43        //In this example we use the utf-8 characterset
44        mail.BodyEncoding = Encoding.GetEncoding("utf-8");
45
46        //send the message
47        SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["MailStr"]);
48        //to authenticate we set the username and password properites on the SmtpClient
49        //smtp.Credentials = new NetworkCredential("username", "secret");
50        //to change the port (default is 25), we set the port property
51        //smtp.Port = 587;
52        //smtp.EnableSsl = true;
53        smtp.Send(mail);
54    }
 
55}

56
带SMTP验证的类
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;

namespace Common
{
    
/// <summary>
    
/// Email 发送类
    
/// </summary>

    public class Email
    
{
        
private static readonly string strMailHost = ConfigurationManager.AppSettings["MailHost"];
        
private static readonly string strMailUserName = ConfigurationManager.AppSettings["MailUserName"];
        
private static readonly string strMailPassWord = ConfigurationManager.AppSettings["MailPassWord"];
        
private static readonly string strMailFromEmail = ConfigurationManager.AppSettings["MailFromEmail"];
        
private static readonly string strMailFromName = ConfigurationManager.AppSettings["MailFromName"];

        
/// <summary>
        
/// 邮件发送函数(静态)
        
/// </summary>
        
/// <param name="mailTo">收件人</param>
        
/// <param name="mailSubject">主题</param>
        
/// <param name="mailBody">邮件内容</param>

        public static void SendMail(string mailTo, string mailSubject, string mailBody)
        
{
            
//create the mail message
            MailMessage mail = new MailMessage();

            
//set the addresses
            mail.From = new MailAddress(strMailFromEmail, strMailFromName);

            mail.To.Add(mailTo);
            
//mail.Bcc.Add("sfwu@cclmotors.com");

            
//set the content
            mail.Subject = mailSubject;
            mail.Body 
= mailBody;
            
//add an attachment from the filesystem
            
//mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));

            mail.IsBodyHtml 
= true;

            
//specify the priority of the mail message
            
//mail.Priority = MailPriority.High;

            
//In this example we use the utf-8 characterset
            mail.BodyEncoding = Encoding.GetEncoding("utf-8");

            
//send the message
            SmtpClient smtp = new SmtpClient(strMailHost);
            smtp.UseDefaultCredentials 
= true;

            
//to authenticate we set the username and password properites on the SmtpClient
            smtp.Credentials = new NetworkCredential(strMailUserName, strMailPassWord);
            
//to change the port (default is 25), we set the port property
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            
//smtp.Port = 587;
            
//smtp.EnableSsl = true;
            smtp.Send(mail);
        }


        
/// <summary>
        
/// 邮件发送函数(静态)
        
/// </summary>
        
/// <param name="mailTo">收件人</param>
        
/// <param name="mailCc">抄送人</param>
        
/// <param name="mailSubject">主题</param>
        
/// <param name="mailBody">邮件内容</param>

        public static void SendMail(string mailTo, string mailCc, string mailSubject, string mailBody)
        
{
            
//create the mail message
            MailMessage mail = new MailMessage();

            
//set the addresses
            mail.From = new MailAddress(strMailFromEmail, strMailFromName);

            mail.To.Add(mailTo);
            mail.CC.Add(mailCc); ;
            
//mail.Bcc.Add("sfwu@cclmotors.com");

            
//set the content
            mail.Subject = mailSubject;
            mail.Body 
= mailBody;
            
//add an attachment from the filesystem
            
//mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));

            mail.IsBodyHtml 
= true;

            
//specify the priority of the mail message
            
//mail.Priority = MailPriority.High;

            
//In this example we use the utf-8 characterset
            mail.BodyEncoding = Encoding.GetEncoding("utf-8");

            
//send the message
            SmtpClient smtp = new SmtpClient(strMailHost);
            smtp.UseDefaultCredentials 
= true;

            
//to authenticate we set the username and password properites on the SmtpClient
            smtp.Credentials = new NetworkCredential(strMailUserName, strMailPassWord);
            
//to change the port (default is 25), we set the port property
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            
//smtp.Port = 587;
            
//smtp.EnableSsl = true;
            smtp.Send(mail);
        }


        
/// <summary>
        
/// 邮件发送函数(静态)
        
/// </summary>
        
/// <param name="mailTo">收件人</param>
        
/// <param name="mailCc">抄送人</param>
        
/// <param name="mailBCc">秘送人</param>
        
/// <param name="mailSubject">主题</param>
        
/// <param name="mailBody">邮件内容</param>

        public static void SendMail(string mailTo, string mailCc, string mailBCc, string mailSubject, string mailBody)
        
{
            
//create the mail message
            MailMessage mail = new MailMessage();

            
//set the addresses
            mail.From = new MailAddress(strMailFromEmail, strMailFromName);

            mail.To.Add(mailTo);
            mail.CC.Add(mailCc);
            mail.Bcc.Add(mailBCc);

            
//set the content
            mail.Subject = mailSubject;
            mail.Body 
= mailBody;
            
//add an attachment from the filesystem
            
//mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));

            mail.IsBodyHtml 
= true;

            
//specify the priority of the mail message
            
//mail.Priority = MailPriority.High;

            
//In this example we use the utf-8 characterset
            mail.BodyEncoding = Encoding.GetEncoding("utf-8");

            
//send the message
            SmtpClient smtp = new SmtpClient(strMailHost);
            smtp.UseDefaultCredentials 
= true;

            
//to authenticate we set the username and password properites on the SmtpClient
            smtp.Credentials = new NetworkCredential(strMailUserName, strMailPassWord);
            
//to change the port (default is 25), we set the port property
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            
//smtp.Port = 587;
            
//smtp.EnableSsl = true;
            smtp.Send(mail);
        }

    }

}

原文地址:https://www.cnblogs.com/cnaspnet/p/515339.html