c# - 为什么不能使用 sendgrid 通过邮件附加附件?
问题描述
当我尝试发送带有位于谷歌驱动器或存储库路径中的附件的电子邮件时,它只发送电子邮件但不发送文件。
我尝试以字节为单位下载文件,然后将其作为附件发送
使用的代码如下
List<Attachment> files = new List<Attachment>
{
new Attachment()
{
Content = "BwdW",
Type = "image/png",
Filename = Server.MapPath("~/Content/IMG/EmailHeader.png"),
Disposition = "inline",
ContentId = "EmailHeader"
}
};
这是方法:
public Boolean EnvioCorreo_Copias_Archivos(string cuerpo, string asunto, string correoEmisor, List<EmailAddress> correoReceptor,
List<Attachment> Archivos)
{
try
{
var clientSendGrid = new SendGridClient("Key_Sendgrid");
var from = new EmailAddress(correoEmisor, "Alias");
List<EmailAddress> tos = correoReceptor;
var body = cuerpo;
var subject = asunto;
var plainTextContent = "";
var htmlContent = body;
var showAllRecipients = true;
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent, showAllRecipients);
msg.AddAttachments(Archivos);
clientSendGrid.SendEmailAsync(msg).Wait();
return true;
}
catch (Exception)
{
return false;
}
}
解决方案
我个人将其用于单个电子邮件附件:
var Message = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var bytes = System.IO.File.ReadAllBytes(AttachmentPath);
var File = Convert.ToBase64String(bytes);
Message.AddAttachment(FileName, File);
APIResponse = await client.SendEmailAsync(Message).ConfigureAwait(false);
推荐阅读
- c++ - C++'speechapi_cxx.h':没有这样的文件或目录 Visual Studio
- java - 如何让动态 JPanel 贴在顶部?
- excel - 如何在公式中使用用作列跟踪器的变量
- flutter - 使行垂直水平
- database - 从本地房间数据库访问数据时出现 kotlin.KotlinNullPointerException?
- javascript - 如何正确地将Child(a)附加到div
- python - 是否可以将此 numpy 函数转换为 tensorflow?
- ruby-on-rails - 为什么“respond_with”不执行失败的设计认证?
- python-3.x - 使用 plt.plot 在 matplotlib 中绘制多个折线图
- java - Java Spring使用RestTemplate反序列化嵌套对象