使用SendGrid发送包含附件的电子邮件

问题描述 投票:8回答:3
 var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("[email protected]");
            myMessage.AddTo("Cristian <[email protected]>");
            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);

基本上我可以使电子邮件工作,并且当我尝试添加附件时它不会发送它。我尝试了不同的路径和不同的写路径方式,我不确定出了什么问题,我发现的每一个教程都表明它应该像这样工作。

c# email smtp email-attachments
3个回答
8
投票

我得到它的工作,结果我只需要一个虚拟的路径:

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

6
投票

\它是一个逃脱角色

//使用常规字符串文字初始化。

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

else //使用逐字字符串文字初始化。

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

3
投票

使用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" };

    }
© www.soinside.com 2019 - 2024. All rights reserved.