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

时间:2016-06-21 13:04:41

标签: c# email smtp email-attachments

 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);

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

5 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:6)

\它是转义字符

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

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

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

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

答案 2 :(得分: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" };

    }

答案 3 :(得分:2)

您可以添加多个文件

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

答案 4 :(得分:2)

这是完整的例子:

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);
        }
    }