溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用C#發(fā)送郵箱

發(fā)布時(shí)間:2021-06-21 18:20:46 來(lái)源:億速云 閱讀:117 作者:Leah 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)如何使用C#發(fā)送郵箱,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

一、簡(jiǎn)單的郵件發(fā)送(不含授權(quán)碼)

    這里不啟動(dòng)安全連接,有些郵箱不支持安全連接。

        /// 最基本的發(fā)送郵件的方法
        /// </summary>
        public static void Send163Demo()
        {
            string user = "asdf@qq.com";//替換成你的hotmail用戶名
            string password = "1234";//替換成你的hotmail密碼
            string host = "smtp.qq.cn";//設(shè)置郵件的服務(wù)器
            string mailAddress = "asdf@qq.com"; //替換成你的hotmail賬戶
            string ToAddress = "lsd@qq.com";//目標(biāo)郵件地址。
            SmtpClient smtp = new SmtpClient(host);
            //smtp.EnableSsl = true; //開(kāi)啟安全連接。
            smtp.Credentials = new NetworkCredential(user, password); //創(chuàng)建用戶憑證
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //使用網(wǎng)絡(luò)傳送
            MailMessage message = new MailMessage(mailAddress, ToAddress, "標(biāo)題", "發(fā)送內(nèi)容");  //創(chuàng)建郵件
            smtp.Send(message); //發(fā)送郵件   異步發(fā)送郵件 smtp.SendAsync(message, "huayingjie"); //這里簡(jiǎn)單修改下,發(fā)送郵件會(huì)變的很快。
            MessageBox.Show("郵件發(fā)送成功!");
        }

二、安全連接發(fā)送郵箱(含授權(quán)碼方式)

  #region QQ郵箱郵件發(fā)送

            //pub.SendMail email = new pub.SendMail(); //引用此類(lèi)所在的命名空間后new一個(gè)對(duì)象出來(lái)
            //string _sendServer = "smtp.qq.com";//服務(wù)器地址
            //string _sendUseremail = "123213@qq.com";//發(fā)件人郵箱
            ////string _sendUserGrant = "hxtl2hbicj";//授權(quán)碼
            //string _sendToUser = "12321323@qq.com";//接收人
            //string _strSubject = "數(shù)字化采購(gòu)系統(tǒng)KPI推送--PP";//主題
            //string _strBody = string.Empty;//發(fā)送內(nèi)容

            //for (int i = 0; i < list_user.Count; i++)
            //{
            //    if (list_user[i].email != "")
            //        _sendToUser += "," + list_user[i].email;
            //}
            ////郵件內(nèi)容頭部
            //_strBody += "大家好! <br /><br /> 以下是PP模塊的KPI匯總內(nèi)容: <br /><br /> ";

            ////中間部分-獲取表格
            //_strBody += getMailBody_PP(a);
            ////郵件內(nèi)容尾部
            //_strBody += "<br /> 請(qǐng)關(guān)注未達(dá)成的內(nèi)容項(xiàng),望可以今日完成。<br />";

            ////email.SendQQMail("smtp.qq.com", "2342@qq.com", "234234", "23432@qq.com", "QQ郵箱服務(wù)器發(fā)送郵件", "用asp.net發(fā)送郵件,用QQ的smtp.qq.com服務(wù)器,測(cè)試成功");

            //email.SendQQMail(_sendServer, _sendUseremail,  _sendToUser, _strSubject, _strBody);

            #endregion
        }

三、含授權(quán)碼服務(wù)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SendMail.pub
{
    class SendMail
    {
        //public void SendQQMail(string strSmtpServer, string strFrom, string strto,
        //    string strSubject, string strBody)
        //{
        //    SmtpClient smtpClient = new SmtpClient();

        //    smtpClient.EnableSsl = true;

        //    smtpClient.UseDefaultCredentials = false;//先設(shè)置

        //    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定電子郵件發(fā)送方式

        //    smtpClient.Host = strSmtpServer; //指定SMTP服務(wù)器

        //    smtpClient.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass); //用戶名和授權(quán)碼

        //    // 發(fā)送郵件設(shè)置

        //    MailMessage mailMessage = new MailMessage(strFrom, strto); // 發(fā)送人和收件人

        //    mailMessage.Subject = strSubject; //主題

        //    mailMessage.Body = strBody;//內(nèi)容
        //    mailMessage.CC.Add("liujihui@shinbada.com");

        //    mailMessage.BodyEncoding = Encoding.UTF8; //正文編碼

        //    mailMessage.IsBodyHtml = true; //設(shè)置為HTML格式

        //    mailMessage.Priority = MailPriority.Low; //優(yōu)先級(jí)

        //    smtpClient.Send(mailMessage);
        //}

        /// <summary>
        /// 發(fā)送郵件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SendALLMail(MailModel model)
        {
            try
            {
                MailAddress receiver = new MailAddress(model.ReceiverAddress, model.ReceiverName);
                MailAddress sender = new MailAddress(model.SenderAddress, model.SenderName);
                MailMessage message = new MailMessage();
                message.From = sender;//發(fā)件人
                message.To.Add(receiver);//收件人
                //message.CC.Add(sender);//抄送人
                message.Subject = model.Title;//標(biāo)題
                message.Body = model.Content;//內(nèi)容
                message.IsBodyHtml = true;//是否支持內(nèi)容為HTML

                SmtpClient client = new SmtpClient();
                client.Host = "smtp.qq.com";
                client.Port = 465;
                client.EnableSsl = true;//是否啟用SSL
                client.Timeout = 10000;//超時(shí)
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(model.SenderAddress, model.SenderPassword);
                client.Send(message);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

}
四、表字段

 public struct MailModel
    {
        /// <summary>
        /// 收件人地址
        /// </summary>
        public string ReceiverAddress { get; set; }
        /// <summary>
        /// 收件人姓名
        /// </summary>
        public string ReceiverName { get; set; }
        /// <summary>
        /// 標(biāo)題
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 內(nèi)容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 發(fā)件人地址(非必填)
        /// </summary>
        public string SenderAddress { get; set; }
        /// <summary>
        /// 發(fā)件人姓名(非必填)
        /// </summary>
        public string SenderName { get; set; }
        /// <summary>
        /// 發(fā)件人密碼(非必填)
        /// </summary>
        public string SenderPassword { get; set; }
        public string host { get; set; }


    }

關(guān)于如何使用C#發(fā)送郵箱就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI