這篇文章將為大家詳細講解有關如何使用C#發送郵箱,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一、簡單的郵件發送(不含授權碼)
這里不啟動安全連接,有些郵箱不支持安全連接。
/// 最基本的發送郵件的方法
/// </summary>
public static void Send163Demo()
{
string user = "asdf@qq.com";//替換成你的hotmail用戶名
string password = "1234";//替換成你的hotmail密碼
string host = "smtp.qq.cn";//設置郵件的服務器
string mailAddress = "asdf@qq.com"; //替換成你的hotmail賬戶
string ToAddress = "lsd@qq.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"); //這里簡單修改下,發送郵件會變的很快。
MessageBox.Show("郵件發送成功!");
}
二、安全連接發送郵箱(含授權碼方式)
#region QQ郵箱郵件發送
//pub.SendMail email = new pub.SendMail(); //引用此類所在的命名空間后new一個對象出來
//string _sendServer = "smtp.qq.com";//服務器地址
//string _sendUseremail = "123213@qq.com";//發件人郵箱
////string _sendUserGrant = "hxtl2hbicj";//授權碼
//string _sendToUser = "12321323@qq.com";//接收人
//string _strSubject = "數字化采購系統KPI推送--PP";//主題
//string _strBody = string.Empty;//發送內容
//for (int i = 0; i < list_user.Count; i++)
//{
// if (list_user[i].email != "")
// _sendToUser += "," + list_user[i].email;
//}
////郵件內容頭部
//_strBody += "大家好! <br /><br /> 以下是PP模塊的KPI匯總內容: <br /><br /> ";
////中間部分-獲取表格
//_strBody += getMailBody_PP(a);
////郵件內容尾部
//_strBody += "<br /> 請關注未達成的內容項,望可以今日完成。<br />";
////email.SendQQMail("smtp.qq.com", "2342@qq.com", "234234", "23432@qq.com", "QQ郵箱服務器發送郵件", "用asp.net發送郵件,用QQ的smtp.qq.com服務器,測試成功");
//email.SendQQMail(_sendServer, _sendUseremail, _sendToUser, _strSubject, _strBody);
#endregion
}
三、含授權碼服務
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;//先設置
// smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定電子郵件發送方式
// smtpClient.Host = strSmtpServer; //指定SMTP服務器
// smtpClient.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass); //用戶名和授權碼
// // 發送郵件設置
// MailMessage mailMessage = new MailMessage(strFrom, strto); // 發送人和收件人
// mailMessage.Subject = strSubject; //主題
// mailMessage.Body = strBody;//內容
// mailMessage.CC.Add("liujihui@shinbada.com");
// mailMessage.BodyEncoding = Encoding.UTF8; //正文編碼
// mailMessage.IsBodyHtml = true; //設置為HTML格式
// mailMessage.Priority = MailPriority.Low; //優先級
// smtpClient.Send(mailMessage);
//}
/// <summary>
/// 發送郵件
/// </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;//發件人
message.To.Add(receiver);//收件人
//message.CC.Add(sender);//抄送人
message.Subject = model.Title;//標題
message.Body = model.Content;//內容
message.IsBodyHtml = true;//是否支持內容為HTML
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
client.Port = 465;
client.EnableSsl = true;//是否啟用SSL
client.Timeout = 10000;//超時
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>
/// 標題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 發件人地址(非必填)
/// </summary>
public string SenderAddress { get; set; }
/// <summary>
/// 發件人姓名(非必填)
/// </summary>
public string SenderName { get; set; }
/// <summary>
/// 發件人密碼(非必填)
/// </summary>
public string SenderPassword { get; set; }
public string host { get; set; }
}
關于如何使用C#發送郵箱就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。