.net Core Mailkit从阵列发送附件

编程入门 行业动态 更新时间:2024-10-25 22:34:06
本文介绍了 Core Mailkit从阵列发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在测试.Net Core MVC,它不支持System.Net.Mail,我发现的唯一选择是Mailkit,它工作正常,但无法弄清楚如何以二进制形式发送已存储在数据库中的附件.我在MVC 5中使用了以下内容:

I am testing .Net Core MVC, which does not support System.Net.Mail, the only alternative I found is Mailkit, works well but can't figure out how to send attachments that I have stored in my database as binary. I used the following in MVC 5:

var mail = new MailMessage(); mail.Attachments.Add(new Attachment(new MemoryStream(attachment), attachmentName, attachmentType));

如何在MailKit中做到这一点.

How can I do it in MailKit.

推荐答案

您需要创建一个构建器,然后向其添加附件,附件可以是IFromFile或二进制字符串. 希望这会有所帮助.

You need to create a builder and then add the attachments to it, the attachments can be either IFromFile or in Binary string. Hope this helps.

public async void SendEmail(string mailTo, string mailFrom, string cc, string subject, string message) { var emailMessage = new MimeMessage(); if (cc != null) { emailMessage.Cc.Add(new MailboxAddress(cc)); } emailMessage.From.Add(new MailboxAddress("SenderName", "UserName")); emailMessage.To.Add(new MailboxAddress("mailTo")); emailMessage.Subject = subject; var builder = new BodyBuilder { TextBody = message }; //Fetch the attachments from db //considering one or more attachments if (attachments != null) { foreach (var attachment in attachments.ToList()) { builder.Attachments.Add(attachmentName, attachment, ContentType.Parse(attachmentType)); } } emailMessage.Body = builder.ToMessageBody(); using (var client = new SmtpClient()) { var credentials = new NetworkCredential { UserName = "USERNAME", Password = "PASSWORD" }; await client.AuthenticateAsync(credentials); await client.ConnectAsync("smtp.gmail", 587).ConfigureAwait(false); await client.SendAsync(emailMessage).ConfigureAwait(false); await client.DisconnectAsync(true).ConfigureAwait(false); } }

更多推荐

.net Core Mailkit从阵列发送附件

本文发布于:2023-11-06 07:50:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   附件   net   Core   Mailkit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!