前言
本文主要给大家介绍了关于.NET发送邮件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
注意:需要找到“POP3/SMTP服务”并开启,然后生成授权码,生成的授权码就是下面登入的密码。
示例代码:
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
35
36
37
38
39
40
41
42
|
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="to">收件人(多人由;隔开)</param>
/// <param name="title">标题</param>
/// <param name="content">内容</param>
/// <param name="cc">抄送</param>
/// <returns></returns>
public string sendEmail( string to, string title, string content, string cc = "" )
{
try
{
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
myMail.From = new System.Net.Mail.MailAddress( "xxx@qq.com" , "xx通知" , System.Text.Encoding.UTF8); //发件人地址,发件人姓名,编码
string [] tos = to.Split( new string [] { ";" }, StringSplitOptions.RemoveEmptyEntries);
for ( int i = 0; i < tos.Length; i++)
{
myMail.To.Add( new System.Net.Mail.MailAddress(tos[i]));
}
string [] ccs = cc.Split( new string [] { ";" }, StringSplitOptions.RemoveEmptyEntries);
for ( int i = 0; i < ccs.Length; i++)
{
myMail.CC.Add( new System.Net.Mail.MailAddress(ccs[i]));
}
myMail.Subject = title;
myMail.SubjectEncoding = Encoding.UTF8;
myMail.Body = content;
myMail.BodyEncoding = Encoding.UTF8;
myMail.IsBodyHtml = true ;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.qq.com" ; smtp.EnableSsl = true ;
smtp.UseDefaultCredentials = false ;
smtp.Credentials = new System.Net.NetworkCredential( "xxx@qq.com" , "password" );
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Send(myMail);
return "" ;
}
catch (Exception ee)
{
return ee.ToString();
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://www.cnblogs.com/smileZeng/p/8487378.html