在网易邮箱或者腾讯邮箱里面都可以开启smtp服务,如图是我的163邮箱:
开启之后,会得到一个新的授权码,是一串英文字符串,相当于你邮箱的另一个密码,如:
ABCABCATZSBLTABC
拿着这串授权码就可以去撸代码了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
namespace TestSendMail
{
public class MailHelp
{
public bool SendMail()
{
bool sendFlg = false;
try
{
SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
//邮件发送方式 通过网络发送到SMTP服务器
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
string sendername = "testhello@163.com";
string password = "ABCABCATZSBLTABC";
smtp.Credentials = new System.Net.NetworkCredential(sendername, password);
MailMessage message = new MailMessage();
message.From = new MailAddress(sendername);
message.To.Add( new MailAddress("110120130@qq.com") );
message.Subject = "标题:新邮件(从163发到qq)";
message.SubjectEncoding = Encoding.UTF8;
message.Body = "新春快乐,万事如意,加油加油";
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
smtp.Send(message);
sendFlg = true;
}
catch (Exception ex)
{
sendFlg = false;
Console.WriteLine(ex.Message);
}
return sendFlg;
}
}
}
运行的时候,调用sendMail方法,如
private void button_Click(object sender, EventArgs e)
{
MailHelp mail = new MailHelp();
mail.SendMail( );
}
尝试一下,代码还是比较简单的。
备注端口资料: