使用SendGrid发送带附件的电子邮件

22
 var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("info@email.com");
            myMessage.AddTo("Cristian <myemail@email.com>");
            myMessage.Subject = user.CompanyName + "has selected you!";
            myMessage.Html = "<p>Hello World!</p>";
            myMessage.Text = "Hello World plain text!";

           // myMessage.AddAttachment("C:\test\test.txt");



            var apiKey = "";
            var transportWeb = new Web(apiKey);
            transportWeb.DeliverAsync(myMessage);

基本上我可以让电子邮件正常工作,但是一旦尝试添加附件,它就无法发送。我尝试了不同的路径和不同的书写方式,我不确定哪里出了问题,因为我找到的每个教程都显示应该像这样工作。


你收到任何错误信息了吗? - Midhun Mundayadan
5个回答

16

我已经解决了问题,原来只需要一个虚拟路径:

myMessage.AddAttachment(Server.MapPath(@"~\img\logo.png"));

8
你可以添加多个文件。
       var msg = MailHelper.CreateSingleEmail(from, to, subject, null, content);

        byte[] byteData = Encoding.ASCII.GetBytes(File.ReadAllText(filePath));
        msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment>
        {
            new SendGrid.Helpers.Mail.Attachment
            {
                Content = Convert.ToBase64String(byteData),
                Filename = "FILE_NAME.txt",
                Type = "txt/plain",
                Disposition = "attachment"
            }
        };

7

\是一个转义字符。

//Initialize with a regular string literal.
myMessage.AddAttachment(@"C:\test\test.txt");

否则
// Initialize with a verbatim string literal.
myMessage.AddAttachment("C:\\test\\test.txt");

5

使用SendGrid附加Blob引用文档

mail.AddAttachment(AzureUploadFileClsName.MailAttachmentFromBlob("DocName20190329141433.pdf"));

你可以按照以下方式创建常见的方法。

public static Attachment MailAttachmentFromBlob(string docpath)
    {
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(storageContainer);
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(docpath);
        blockBlob.FetchAttributes();
        long fileByteLength = blockBlob.Properties.Length;
        byte[] fileContent = new byte[fileByteLength];
        for (int i = 0; i < fileByteLength; i++)
        {
            fileContent[i] = 0x20;
        }
        blockBlob.DownloadToByteArray(fileContent, 0);

        return new Attachment{ Filename = "Attachmentname",
            Content = Convert.ToBase64String(fileContent),
            Type = "application/pdf",
            ContentId = "ContentId" };

    }

3

以下是完整示例:

static async Task ExecuteStreamAttachmentAdd()
    {
        var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("test@example.com");
        var subject = "Subject";
        var to = new EmailAddress("test@example.com");
        var body = "Email Body";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, body, "");

        using (var fileStream = File.OpenRead("C:\\Users\\username\\file.txt"))
        {
            await msg.AddAttachmentAsync("file.txt", fileStream);
            var response = await client.SendEmailAsync(msg);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接