4

In blobs, I have generated xlsx report that I can read. I want to send this report in mail. For this I am using SendGrid. I get this report as stream from blob, create Attachment and try to send it in mail. As result I receive mail with attachment, but I cannot read it.

private async Task SendReportAsync(string fileName)
{
    var reportStream = await blobHelper.GetBlobStreamAsync(fileName).ConfigureAwait(false);
    var message = new Message
    {
        HtmlBody = "Please see file attached", Subject = "Test Report with Attachment", TextBody = "Please see file attached"
    };
    var sender = new MessageAddress
    {
       Address = "noreply@mail.com", Name = "test"
    };

    var attachment = new Attachment(reportStream, fileName);

    await myMessageService.SendAsync(message, sender, new MessageAddress { Address = "mail.receiver@mail.com" }, new[] { attachment });
}

myMessageService:

public async Task SendAsync(
        Message message,
        MessageAddress sender,
        MessageAddress recipient,
        NameValueCollection headers = null,
        MessageAddress bccRecipient = null,
        IEnumerable<Attachment> attachments = null)
{
    var mailMsg = this.PrepareMailMessage(
            message,
            sender,
            recipient,
            headers,
            bccRecipient,
            out SmtpClient smtpClient,
            attachments);

    await smtpClient.SendMailAsync(mailMsg).ConfigureAwait(false);
}

private MailMessage PrepareMailMessage(
        IMessage message,
        IMessageAddress sender,
        IMessageAddress recipient,
        NameValueCollection headers,
        IMessageAddress bccRecipient,
        out SmtpClient smtpClient,
        IEnumerable<Attachment> attachments = null)
{
    var mailMsg = new MailMessage();

    // Assigning all available values to mailMsg

    return mailMsg;
}
demo
  • 6,038
  • 19
  • 75
  • 149

0 Answers0