crystyxn
crystyxn

Reputation: 1601

Sending an email with attachment using SendGrid

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

Basically I can make the email work, and the moment I attempt to add an attachment it doesn't send it. I tried different paths and different ways of writing the path, I am not sure what is going wrong, every single tutorial I have found shows it should work like this.

Upvotes: 24

Views: 39174

Answers (5)

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

\ it is a escape character

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

else

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

Upvotes: 7

Ashish Tripathi
Ashish Tripathi

Reputation: 666

This's the complete example:

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("[email protected]");
        var subject = "Subject";
        var to = new EmailAddress("[email protected]");
        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);
        }
    }

Upvotes: 3

Recep Duman
Recep Duman

Reputation: 366

You can add multiple files

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

Upvotes: 9

PradipPatel1411
PradipPatel1411

Reputation: 139

attach blob reference doc using sendgrid

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

common method you can create as below one.

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

    }

Upvotes: 5

crystyxn
crystyxn

Reputation: 1601

I got it to work, turns out I just needed a virtual path:

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

Upvotes: 16

Related Questions