话说网上发送邮件的代码很多,但是我由于不细心,导致拿别人的代码发送邮件老是失败,今天就说说几个要注意的地方吧!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public bool SendEmail()
{
MailMessage msg = new MailMessage();
msg.To.Add( "to@qq.com" ); //收件人地址
msg.CC.Add( "cc@qq.com" ); //抄送人地址
msg.From = new MailAddress( "from@qq.com" , "Edward" ); //发件人邮箱,名称
msg.Subject = "This is a test email from QQ" ; //邮件标题
msg.SubjectEncoding = Encoding.UTF8; //标题格式为UTF8
msg.Body = "this is body" ; //邮件内容
msg.BodyEncoding = Encoding.UTF8; //内容格式为UTF8
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com" ; //SMTP服务器地址
client.Port = 587; //SMTP端口,QQ邮箱填写587
client.EnableSsl = true ; //启用SSL加密
//发件人邮箱账号,授权码(注意此处,是授权码你需要到qq邮箱里点设置开启Smtp服务,然后会提示你第三方登录时密码处填写授权码)
client.Credentials = new System.Net.NetworkCredential( "from@qq.com" , "password" );
try
{
client.Send(msg); //发送邮件
}
catch (Exception)
{
return false ;
}
return true ;
}
|
网上类似这段代码的代码很多,但是大多数都说的是client.Credentials = new System.Net.NetworkCredential("from@qq.com", "password")这块写的是password,其实可能之前的前辈们用的时候QQ邮箱需要的是密码,但是现在qq邮箱需要的则是授权码,下面就说怎么拿到这个授权码吧!
首先发送邮件要开启一个smtp的服务(ps:我这菜鸟还是不久前才知道的,不过好像outlook不是smtp,反正协议很多的)
第一步:打开qq邮箱点击设置:
然后还有两步你就成功了
第二步:点击账户
第三步:向下拉找到smtp字样的地方,然后全部开启
相信你能理解这句话,然后你开启的时候会自动弹出授权码,如果没有当时没有复制粘贴可以点击生成授权码 ,来重新生成:
就是这个东东了,拿这个复制粘贴到你代码的password处即可(ps:本菜鸟被坑的很惨啊,再说多一句163的邮箱也是这样的,只有开启smtp服务才能拿到授权码)
1
|
<em id= "__mceDel" ><em id= "__mceDel" ><span style= 'color: rgb(255, 0, 0); font-family: "Microsoft YaHei"; font-size: 16px;' > </span></em></em>
|
标
==========================================================================================================
.NET 里包含了很多很丰富的邮件发送与接受的API在 System.Net.Mail命名空间里,使得我们开发发送和接受邮件相关功能变得简单,下面是一个简单发送邮件的功能:
1 private void btn_send_Click(object sender, EventArgs e)
2 {
3 var emailAcount = ConfigurationManager.AppSettings["EmailAcount"];
4 var emailPassword = ConfigurationManager.AppSettings["EmailPassword"];
5 var reciver = txt_Reciver.Text;
6 var content = rtxt_Content.Text;
7 MailMessage message = new MailMessage();
8 //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
9 MailAddress fromAddr = new MailAddress("762004031@qq.com");
10 message.From = fromAddr;
11 //设置收件人,可添加多个,添加方法与下面的一样
12 message.To.Add(reciver);
13 //设置抄送人
14 message.CC.Add("izhaofu@163.com");
15 //设置邮件标题
16 message.Subject = "Test";
17 //设置邮件内容
18 message.Body = content;
19 //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是QQ的
20 SmtpClient client = new SmtpClient("smtp.qq.com", 25);
21 //设置发送人的邮箱账号和密码
22 client.Credentials = new NetworkCredential(emailAcount,emailPassword);
23 //启用ssl,也就是安全发送
24 client.EnableSsl = true;
25 //发送邮件
26 client.Send(message);
27 }
测试一下效果:
再看一下收件邮箱:
发件箱中的发件记录:
- 发送邮件所用的核心知识点
- 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
- 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
- SmtpClient主要进行了三层的封装:Socket --> TcpClient --> SmtpClient
- 具体代码请看如下所示:
public class MyEmail
{
private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25)
private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可)
private string mSenderPassword; //发件箱的密码
private string mSenderUsername; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
private bool mEnableSsl; //是否对邮件内容进行socket层加密传输
private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 ///<summary>
/// 构造函数
///</summary>
///<param name="server">发件箱的邮件服务器地址</param>
///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
///<param name="fromMail">发件人地址</param>
///<param name="subject">邮件标题</param>
///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param>
///<param name="password">发件人邮箱密码</param>
///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
{
try
{
mMailMessage = new MailMessage();
mMailMessage.To.Add(toMail);
mMailMessage.From = new MailAddress(fromMail);
mMailMessage.Subject = subject;
mMailMessage.Body = emailBody;
mMailMessage.IsBodyHtml = true;
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMailMessage.Priority = MailPriority.Normal;
this.mSenderServerHost = server;
this.mSenderUsername = username;
this.mSenderPassword = password;
this.mSenderPort = Convert.ToInt32(port);
this.mEnableSsl = sslEnable;
this.mEnablePwdAuthentication = pwdCheckEnable;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} public void Send()
{
try
{
if (mMailMessage != null)
{
mSmtpClient = new SmtpClient();
//mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
mSmtpClient.Host = this.mSenderServerHost;
mSmtpClient.Port = this.mSenderPort;
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.EnableSsl = this.mEnableSsl;
if (this.mEnablePwdAuthentication)
{
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
//mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
//NTLM: Secure Password Authentication in Microsoft Outlook Express
mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
}
else
{
mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
}
mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mSmtpClient.Send(mMailMessage);
}
}
catch (Exception ex)
{
//m_CreateErrorLogTxt("程序池(Email)通知", "邮件发送失败", ex.Message);
}
} }
转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html
/// <summary>
/// 发送邮件通知
/// </summary>
/// <param name="subjectInfo">标题</param>
/// <param name="bodyInfo">内容</param>
public static void Email(string subjectInfo, string bodyInfo)
{
try
{
var appSettings = System.Configuration.ConfigurationManager.AppSettings;//获取config配置信息
string senderServerIp = "mail.zbian.cn";
string toMailAddress = appSettings["toMailAddress"];//接受方地址
string fromMailAddress = appSettings["fromMailAddress"];//发送方地址
string mailUsername = appSettings["mailUsername"]; //发送账户
string mailPassword = appSettings["mailPassword"]; //发送邮箱的密码
string mailPort = "25";
MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
email.Send();
}
catch (Exception ex)
{
}
}
///添加config配置信息
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("IsFirstInstall", "true");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings"); ///获取config配置信息
var appSettings = System.Configuration.ConfigurationManager.AppSettings;