Sending an email with attachment using SendGrid

后端 未结 4 1902
攒了一身酷
攒了一身酷 2020-12-06 06:01
 var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress(\"info@email.com\");
            myMessage.AddTo(\"Cristian 

        
相关标签:
4条回答
  • 2020-12-06 06:12

    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"
                }
            };
    
    0 讨论(0)
  • 2020-12-06 06:17

    \ 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");
    
    0 讨论(0)
  • 2020-12-06 06:22

    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" };
    
        }
    
    0 讨论(0)
  • 2020-12-06 06:23

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

    myMessage.AddAttachment(Server.MapPath(@"~\img\logo.png"));
    
    0 讨论(0)
提交回复
热议问题