gpt4 book ai didi

angular - 将 IFormFile 附加到邮件而不保存文件

转载 作者:行者123 更新时间:2023-12-02 02:57:11 27 4
gpt4 key购买 nike

是否可以将 IFormFile 文件添加到 .net core 中的电子邮件附件中?我正在使用 formdata 从 Angular 获取文件。

 for (let file of this.files) {
this.formData.append("Files", file.nativeFile);
}

MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient
{
Host = "smtp.sendgrid.net",
Port = 25,
Credentials = new System.Net.NetworkCredential("key", "pass")
};


[HttpPost("[action]")]
public IActionResult UploadFiles(IList<IFormFile> Files)
{
foreach (var file in Files)
{
using (var stream = file.OpenReadStream())
{
var attachment = new Attachment(stream, file.FileName);
mail.Attachments.Add(attachment);
}
}
mail.To.Add("email@hotmail.com");
mail.From = from;
mail.Subject = "Subject";
mail.Body = "test";
mail.IsBodyHtml = true;
smtp.Send(mail);

最佳答案

我现在可以将 IFormFile 文件附加到邮件,而无需将文件保存到服务器。我正在将文件转换为字节数组。我转换为字节数组的原因是我的网站位于 Azure 中,而 Azure 将文件转换为字节数组。否则我无法打开pdf文件。它抛出以下错误:...它作为电子邮件附件发送,但未正确解码。

enter image description here

工作代码:

[HttpPost("[action]")]
public IActionResult UploadFiles(IList<IFormFile> Files)
{
foreach (var file in Files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
var fileBytes = ms.ToArray();
Attachment att = new Attachment(new MemoryStream(fileBytes), file.FileName);
mail.Attachments.Add(att);
}
}
}
mail.To.Add("someemamil@hotmail.com");
mail.From = from;
mail.Subject = "subject";
mail.Body = "test";
mail.IsBodyHtml = true;
smtp.Send(mail);
mail.Dispose();

关于angular - 将 IFormFile 附加到邮件而不保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com