C#發送郵件源碼


介紹

SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。
SMTP協議屬於TCP/IP協議簇,它幫助每台計算機在發送或中轉信件時找到下一個目的地。
SMTP服務器則是遵循SMTP協議的發送郵件服務器,用來發送或中轉發出的電子郵件。

發送郵件的基本步驟

第一:需要指明郵件SMTP服務器地址:smtp.163.com
第二:開啟SSL安全連接。
第三:用戶憑證。

SmtpClient是什么

Socket → TcpListener → SmtpClient

簡單的郵件發送

        /// <summary>
        /// 最基本的發送郵件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "*****@163.com";//替換成你的hotmail用戶名
            string password = "******";//替換成你的hotmail密碼
            string host = "smtp.163.com";//設置郵件的服務器
            string mailAddress = "*****@163.com"; //替換成你的hotmail賬戶
            string ToAddress = "*******@163.com";//目標郵件地址。

            SmtpClient smtp = new SmtpClient(host);
            smtp.EnableSsl = true; //開啟安全連接。
            smtp.Credentials = new NetworkCredential(user, password); //創建用戶憑證
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //使用網絡傳送
            MailMessage message = new MailMessage(mailAddress, ToAddress, "標題", "發送內容");  //創建郵件
            smtp.Send(message); //發送郵件   異步發送郵件 smtp.SendAsync(message, "huayingjie"); //這里簡單修改下,發送郵件會變的很快。
            Console.WriteLine("郵件發送成功!");
        }

下面是對代碼的封裝,進行對hotmail、163、QQ發送郵件

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

namespace SendMail
{
    public class MailModel
    {
        /// <summary>
        /// 用戶名,根據需要看是否需要帶@hotmail.com
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密碼
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 服務器
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// 郵箱賬戶
        /// </summary>
        public string FromMailAddress { get; set; }

        /// <summary>
        /// 目標賬戶
        /// </summary>
        public IList<string> ToAddress { get; set; }

        /// <summary>
        /// 是否開啟安全連接
        /// </summary>
        public bool EnableSsl { get; set; }

        /// <summary>
        /// 傳輸協議
        /// </summary>
        public SmtpDeliveryMethod DeliveryMethod { get; set; }

        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 標題
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 內容
        /// </summary>
        public string Body { get; set; }

        public MailModel() {
            ToAddress = new List<string>();
        }

        public MailModel(
            string userName,
            string password,
            string host,
            string mailAddress,
            IList<string> toAddress,
            bool enableSsl,
            SmtpDeliveryMethod deliveryMethod,
            int port,
            string title,
            string content
            )
        {
            UserName = userName;
            Password = password;
            Host = host;
            FromMailAddress = mailAddress;
            ToAddress = toAddress;
            EnableSsl = enableSsl;
            DeliveryMethod = deliveryMethod;
            Port = port;
            Subject = title;
            Body = content;
        }
    }
}
using SendMail;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;

namespace SendMailDemo
{
    class SendMail
    {
        public SendMail() { }

        /// <summary>
        /// 最基本的發送郵件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "*****@163.com";//替換成你的hotmail用戶名
            string password = "******";//替換成你的hotmail密碼
            string host = "smtp.163.com";//設置郵件的服務器
            string mailAddress = "*****@163.com"; //替換成你的hotmail賬戶
            string ToAddress = "*******@163.com";//目標郵件地址。

            SmtpClient smtp = new SmtpClient(host);
            smtp.EnableSsl = true; //開啟安全連接。
            smtp.Credentials = new NetworkCredential(user, password); //創建用戶憑證
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //使用網絡傳送
            MailMessage message = new MailMessage(mailAddress, ToAddress, "標題", "發送內容");  //創建郵件
            smtp.Send(message); //發送郵件
            Console.WriteLine("郵件發送成功!");
        }

        //可以
        public static void SendHotmail()
        {
            string user = "*****@hotmail.com";
            string password = "*****";
            string host = "smtp.live.com";
            string mailAddress = "*****@hotmail.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@163.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "標題";
            string content = "發送內容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel);
        }

        //可以
        public static void Send163()
        {
            string user = "*****@163.com";
            string password = "*****";
            string host = "smtp.163.com";
            string mailAddress = "*****@163.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@163.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "標題";
            string content = "發送內容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel);
        }

        public static void SendQQ()
        {
            string user = "*****@qq.com";
            string password = "*****";
            string host = "smtp.qq.com";
            string mailAddress = "*****@qq.com";
            IList<string> toAddress = new List<string>();
            toAddress.Add("*****@qq.com");
            bool enableSsl = true;
            SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network;
            int post = 465;
            string title = "標題";
            string content = "發送內容";
            MailModel mailModel = new MailModel(user, password, host, mailAddress, toAddress,
                enableSsl, deliveryMethod, post, title, content);

            SennMail(mailModel); ;
        }

        public static void SennMail(MailModel mailModel)
        {
            SmtpClient smtp = new SmtpClient(mailModel.Host);
            smtp.EnableSsl = mailModel.EnableSsl;
            smtp.Credentials = new NetworkCredential(mailModel.UserName, mailModel.Password);
            smtp.DeliveryMethod = mailModel.DeliveryMethod;
            //smtp.Port = mailModel.Port;//有些時候設置端口會出現很奇怪的問題,這個與服務器的設置有關,建議不要設置端口。
            MailAddress mailAddress = new MailAddress(mailModel.FromMailAddress);

            MailMessage message = new MailMessage();
            message.From = mailAddress;
            foreach (string toAddress in mailModel.ToAddress)
            {
                message.To.Add(toAddress);
            }
            message.Subject = mailModel.Subject;
            message.Body = mailModel.Body;
            //message.IsBodyHtml = true;
            smtp.Send(message);
            Console.WriteLine("郵件發送成功!");
        }
    }
}
using System;

namespace SendMailDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SendMail.Send163Demo();
            //SendMail.Send163();
            //SendMail.SendQQ();
            //SendMail.SendHotmailDemo();
            Console.ReadLine();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM