c# 发邮件

用c#发邮件的简单写法

1:先定义一个EmailHelper类

 public class EmailHelper
    {
        public static void SendMail(string from, string to, string subject, string body, string attachmentFilePath)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(from);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.Body = body;
            message.Attachments.Add(new Attachment(attachmentFilePath));

            SmtpClient smtp = new SmtpClient();
       //不同的邮箱的smtp服务器地址都是不同的,如qq的是smtp.qq.com smtp.Host
= "smtp.163.com"; smtp.Port = 25; smtp.Credentials = new System.Net.NetworkCredential("邮箱账号(发送邮箱)", "密码"); smtp.Send(message); } }

2:调用

 EmailHelper.SendMail("邮箱账号(发送邮箱)", "邮箱账号(接收邮箱)", "标题", "内容", "附件地址,没有就null");

3:设置发送邮箱的smtp权限,网易邮箱的设置如下

点击设置-》选择pop3/smtp/imap

进入这个页面后勾上pop3/smtp和imap/smtp服务然后保存

最后就大功告成啦!

原文地址:https://www.cnblogs.com/yzw-carrie/p/5660507.html