C# 发QQ邮件 (个人邮箱)
SendMail 类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
namespace PC
{
public class SendMail
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string password;//发件人密码
private string _Host = "";
private int _Port = 25;
///
/// 设置MailMessage的实例
///
/// 收件人地址
/// 发件人地址
/// 邮件正文
/// 邮件的主题
/// 发件人密码
public SendMail(string[] To, string From, string Body, string Title, string Password,string Host="",int Port=25)
{
mailMessage = new MailMessage();
foreach (var item in To)
{
mailMessage.To.Add(item);
}
mailMessage.From = new System.Net.Mail.MailAddress(From);
mailMessage.Subject = Title;
mailMessage.Body = Body;
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
_Host = Host;
_Port = Port;
this.password = Password;
}
///
/// 添加附件
///
public void Attachments(string Path)
{
string[] path = Path.Split(',');
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期
mailMessage.Attachments.Add(data);//添加到附件中
}
}
#region 异步发送邮件
///
/// 异步发送邮件
///
///
public void SendAsync(SendCompletedEventHandler SendCompletedCallback)
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;// 指定 System.Net.Mail.SmtpClient 是否使用安全套接字层 (SSL) 加密连接,必须在实例身份前面设置
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host =_Host;
smtpClient.Port = _Port;
smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);//注册异步发送邮件完成时的事件
smtpClient.SendAsync(mailMessage, mailMessage.Body);
}
}
#endregion
///
/// 发送邮件
///
public void Send()
{
if (mailMessage != null)
{
using (smtpClient = new SmtpClient())
{
smtpClient.EnableSsl = true;// 指定 System.Net.Mail.SmtpClient 是否使用安全套接字层 (SSL) 加密连接,必须在实例身份前面设置
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = _Host;
smtpClient.Port = _Port;
smtpClient.Send(mailMessage);
mailMessage.Attachments.Dispose();
}
}
}
}
}
**需要注意的地方
邮箱 host, Port (看下面的 常用邮箱的服务器(SMTP/POP3)地址和端口)
QQ个人邮箱
SMTP服务器地址:smtp.qq.com (端口:25)
发件邮箱密码(个人QQ邮箱为授权码,QQ企业邮箱则为QQ密码)**
QQ邮箱授权码开通
调用代码
public void SendEmail(string sBody)
{
string FormMail = "";// 发件邮箱
string sPassWord = "";// 发件邮箱密码(个人QQ邮箱为授权码,QQ企业邮箱则为QQ密码)
List<string> lsToMail = new List<string>();//收件邮箱
lsToMail.Add(""1234567890@qq.com"");
lsToMail.Add(""987654321@qq.com"");
SendMail sendmail = new SendMail(lsToMail.ToArray(), FormMail, sBody.ToString(), "Tilte", sPassWord, "smtp.qq.com",25);
sendmail.SendAsync(SendCompletedCallback);
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("已取消邮件发送");
return;
}
if (e.Error != null)
{
MessageBox.Show("邮件发送失败");
return;
}
else
{
MessageBox.Show("邮件发送成功");
return;
}
}
#endregion
常用邮箱的服务器(SMTP/POP3)地址和端口
163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110)
SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:SMTP.139.com(端口:25)
QQ邮箱:
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱 :
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
gmail(google.com) :
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587 )
HotMail :
POP3服务器地址:pop3.live.com (端口:995)
SMTP服务器地址:smtp.live.com (端口:587)
263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com