为什么不能通过sendgrid邮件附加附件?

问题描述 投票:-1回答:1

当我尝试发送包含位于Google云端硬盘或存储库路径中的附件的电子邮件时,它只发送电子邮件但没有文件。

我尝试以字节为单位下载文件,然后将其作为附件发送

使用的代码如下

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;
        }
    }
c# email attachment sendgrid
1个回答
0
投票

我刚刚确认这适用于SendGrid,附件工作得很好:

var msg = new MailMessage(fromAddress, toAddress)
{
  Subject = "attachment test", 
  IsBodyHtml = true, 
  Body = "this is the body"
};

var attachment = new Attachment(attachmentPath);
msg.Attachments.Add(attachment);

using (var client = new SmtpClient("smtp.sendgrid.net"))
{
  client.UseDefaultCredentials = false;
  client.EnableSsl = true;
  client.Port = 587;
  client.Credentials = new NetworkCredential("key","password");

  await client.SendMailAsync(msg);
}
© www.soinside.com 2019 - 2024. All rights reserved.