.net core 如何发送邮件
首页你得找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务:
然后开启
下面是代码实现邮箱推送了。
/// <summary>
/// 邮件推送
/// </summary>
/// <returns></returns>
[Obsolete]
public static async Task SendMailAsync(string email,string Content,string PushType,string filder)
{
string smtpService = "smtp.qq.com";
string sendEmail = "你的qq号@qq.com";
string sendpwd = "kcbglwpdsqxjceda";//你开启qq设置的时候,会生成一个key值,复制过来就行了。
//确定smtp服务器地址 实例化一个Smtp客户端
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = smtpService;
//确定发件地址与收件地址
MailAddress sendAddress = new MailAddress(sendEmail);
MailAddress receiveAddress = new MailAddress(email);
//构造一个Email的Message对象 内容信息
MailMessage mailMessage = new MailMessage(sendAddress, receiveAddress);
mailMessage.Subject = PushType + DateTime.Now;
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailMessage.Body = Content;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
//邮件发送方式 通过网络发送到smtp服务器
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
//如果服务器支持安全连接,则将安全连接设为true
smtpclient.EnableSsl = true;
try
{
//是否使用默认凭据,若为false,则使用自定义的证书,就是下面的networkCredential实例对象
smtpclient.UseDefaultCredentials = false;
//指定邮箱账号和密码,需要注意的是,这个密码是你在QQ邮箱设置里开启服务的时候给你的那个授权码
NetworkCredential networkCredential = new NetworkCredential(sendEmail, sendpwd);
smtpclient.Credentials = networkCredential;
//发送邮件
smtpclient.Send(mailMessage);
Console.WriteLine("发送邮件成功");
}
catch (System.Net.Mail.SmtpException ex)
{
Console.WriteLine(ex.Message, "发送邮件出错");
}
}