• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# SmtpClient发送邮件以和Mailkit 邮件发送包含附件

武飞扬头像
新笙
帮助1

在进行微信小程序以及手机app开发时,作为后端就需要进行接口的开发。

像平常一样部署的将数据以邮件形式发送

接口代码内容如下:

  1.  
    [WebMethod(Description = "下载到邮箱")]
  2.  
    public string SendQQMail(string key, string strto, string strfile, string strfileName)//key是为了安全校验,strto是接收者的邮箱,strfile为附件的路径,strfileName附件名称
  3.  
    {
  4.  
    string systemKey = "37a3ab25-cd1a-481b-a675-87161b20c19e";
  5.  
    if (key == systemKey)
  6.  
    {
  7.  
    try
  8.  
    {
  9.  
    string strFrom = "xxx@qq.com";
  10.  
    string strSubject = "发送测试";
  11.  
    string strFromName = "发送测试";
  12.  
    string strBody = "发送测试";
  13.  
    //企业邮箱smtp
  14.  
    string strSmtpServer = "smtp.qq.com";
  15.  
    string strFromPass = "xxxxxxx";//授权码
  16.  
    SmtpClient smtpClient = new SmtpClient();
  17.  
    smtpClient.EnableSsl = true;
  18.  
    smtpClient.UseDefaultCredentials = false;//先设置
  19.  
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定电子邮件发送方式
  20.  
    smtpClient.Host = strSmtpServer; //指定SMTP服务器
  21.  
    smtpClient.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass); //用户名和授权码
  22.  
    // 发送邮件设置
  23.  
    //MailMessage mailMessage = new MailMessage(strFrom, strto); // 发送人和收件人
  24.  
    MailMessage mailMessage = new MailMessage();
  25.  
    //发件人地址
  26.  
    //如是自己,在此输入自己的邮箱
  27.  
    mailMessage.From = new MailAddress(strFrom, strFromName, Encoding.UTF8);
  28.  
    //收件人地址
  29.  
    mailMessage.To.Add(new MailAddress(strto));
  30.  
    // 添加附件
  31.  
    if (strfile != "")
  32.  
    {
  33.  
    {
  34.  
    Attachment mailAttach_1 = new Attachment(strfile);
  35.  
    if (strfile.Contains('.'))
  36.  
    {
  37.  
    mailAttach_1.Name = strfileName strfile.Substring(strfile.LastIndexOf('.'));
  38.  
    }
  39.  
    else mailAttach_1.Name = strfileName ".xlsx";
  40.  
    mailMessage.Attachments.Add(mailAttach_1);
  41.  
    }
  42.  
    mailMessage.Subject = strSubject; //主题
  43.  
    mailMessage.Body = strBody;//内容
  44.  
    mailMessage.BodyEncoding = Encoding.UTF8; //正文编码
  45.  
    mailMessage.IsBodyHtml = true; //设置为HTML格式
  46.  
    mailMessage.Priority = MailPriority.Low; //优先级
  47.  
    smtpClient.Send(mailMessage);
  48.  
    Console.WriteLine();
  49.  
    Console.ReadLine();
  50.  
    return "发送成功";
  51.  
    }
  52.  
    catch (Exception ex)
  53.  
    {
  54.  
    System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\log.txt");
  55.  
    file.WriteLine(ex.StackTrace);
  56.  
    file.Close();
  57.  
    return ex.Message;
  58.  
    }
  59.  
    }
  60.  
    else
  61.  
    {
  62.  
    return "";
  63.  
    }
  64.  
    }
学新通

小程序在部署iis的时候需要tls协议,主流的邮箱服务商加密协议使用的都是TLS。
但是System.Net.Mail.SmtpClient 不支持较新的TLS协议因此需要使用另一个

MailKit是一个开源的基于MimeKit的跨平台邮件收发类库,支持IMAP、POP3、SMTP。其中SmtpClient也支持TLS协议.

可以很好的支持 .NET Core以及 .NET Framework框架的邮件发送

  1.  
    using MailKit.Net.Smtp;
  2.  
    using MailKit;
  3.  
    using MimeKit;

接口代码如下:

  1.  
    [WebMethod(Description = "下载到邮箱")]
  2.  
    public string SendQQMail(string key, string strto, string strfile, string strfileName)//key是为了安全校验,strto是接收者的邮箱,strfile为附件的路径,strfileName附件名称
  3.  
    {
  4.  
    string systemKey = "37a3ab25-cd1a-481b-a675-87161b20c19e";
  5.  
    if (key == systemKey)
  6.  
    {
  7.  
    try
  8.  
    {
  9.  
    var message = new MimeMessage();
  10.  
    message.From.Add(new MailboxAddress("test", "xxxxxx@qq.com"));//发送人的名称以及邮箱
  11.  
    message.To.Add(new MailboxAddress("test", strto));//接收人的名称以及邮箱
  12.  
    message.Subject = "发送测试";//邮件主题
  13.  
    //html or plain
  14.  
    var bodyBuilder = new BodyBuilder();
  15.  
    bodyBuilder.HtmlBody = "<b>发送测试</b>";
  16.  
    bodyBuilder.TextBody = "发送测试";
  17.  
    message.Body = bodyBuilder.ToMessageBody();
  18.  
    Multipart multipart = new Multipart("mixed");
  19.  
    //附件
  20.  
    string absolutePath = strfile;
  21.  
    MimePart attachment = new MimePart()
  22.  
    {
  23.  
    //读取文件,只能用绝对路径
  24.  
    ContentObject = new ContentObject(File.OpenRead(absolutePath), ContentEncoding.Default),
  25.  
    ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
  26.  
    ContentTransferEncoding = ContentEncoding.Base64,
  27.  
    //文件名字
  28.  
    FileName = strfileName ".xlsx"
  29.  
    };
  30.  
    multipart.Add(attachment);
  31.  
    message.Body = multipart;
  32.  
    using (var client = new MailKit.Net.Smtp.SmtpClient())
  33.  
    {
  34.  
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
  35.  
    //smtp服务器,端口,是否开启ssl
  36.  
    client.Connect("smtp.qq.com", 465, true);
  37.  
    client.Authenticate("xxxxx@qq.com", password);//发送者邮箱以及授权码
  38.  
    client.Send(message);
  39.  
    client.Disconnect(true);
  40.  
    }
  41.  
    return "发送成功";
  42.  
    }
  43.  
    catch (Exception ex)
  44.  
    {
  45.  
    System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\log.txt");
  46.  
    file.WriteLine(ex.StackTrace);
  47.  
    file.Close();
  48.  
    return ex.Message;
  49.  
    }
  50.  
    }
  51.  
    else
  52.  
    {
  53.  
    return "";
  54.  
    }
  55.  
    }
学新通

要使用需要安装打开vs2019 Nuget 搜索一下两个包 两个m开头的

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiibbf
系列文章
更多 icon
同类精品
更多 icon
继续加载