Search code examples
c#angularasp.net-core-webapijspdfmailkit

.Net Core 5 web api Email Attachment using mimekit and mailkit


I have a file that is coming in from client side as this:

ContentType = "application/octet-stream"

of which is a pdf file. What seems to be happening is that I am not getting file from the stream and it's becoming corrupt when opening the attachment in email. Here is my code:

Client Side

Angular:

createPdf(): {
  var dataURI = doc.output('datauristring');
  return dataURI ? dataURI : "";
}

createMail(): void {
    var blob = new Blob([this.createPdf()], { type: 'application/pdf' });
    var fileOfBlob = new File([blob], this.data.accountName + '_' + this.data.exceptionDate + '_' + 'ExceptionRequest.pdf');
    const mail: FormData = new FormData();
    mail.append('ToEmail', this.data.contactEmail);
    mail.append('Subject', 'Test');
    mail.append('Body', 'Test Email');
    mail.append('Attachments', fileOfBlob);

    console.log(fileOfBlob);

    this.mailService.createMail(mail)
      .subscribe(res => {
        console.log(res);
      });
  }

Server Side:

Model:

public class MailRequest
    {
        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public IFormFile Attachments { get; set; }
    }

Service:

        public async Task SendEmailAsync(MailRequest mailRequest)
        {
            var email = new MimeMessage();
            email.Sender = MailboxAddress.Parse(_mailSettings.Mail);
            email.To.Add(MailboxAddress.Parse(mailRequest.ToEmail));
            email.Subject = mailRequest.Subject;

            var builder = new BodyBuilder();
            byte[] fileBytes;
            if (mailRequest.Attachments != null)
            {
                var file = mailRequest.Attachments;
                if (file.Length > 0)
                {
                    using (var ms = new MemoryStream())
                    {
                        file.CopyTo(ms);
                        fileBytes = ms.ToArray();
                    }
                    builder.Attachments.Add(file.FileName, fileBytes, MimeKit.ContentType.Parse(MediaTypeNames.Application.Pdf));
                }
            }
            builder.HtmlBody = mailRequest.Body;
            email.Body = builder.ToMessageBody();
            using var smtp = new SmtpClient();
            smtp.Connect(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls);
            smtp.Authenticate(_mailSettings.Mail, _mailSettings.Password);
            await smtp.SendAsync(email);
            smtp.Disconnect(true);
        }

Controller:

[HttpPost("send")]
        public async Task<IActionResult> SendMail([FromForm] MailRequest request)
        {
            try
            {
                await _mailService.SendEmailAsync(request);
                return Ok();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

I am thinking that I need to parse the stream to pdf but not sure where in my code that it is failing exactly. Does anyone have any insight to what I may be doing wrong within the SendEmailAsync service above?


Solution

  • The answer is that I was not passing it as a blob on client side in function createPdf(). So, what I needed to do is pass it like this:

    Angular code:

    createPdf(): {
      var blob = doc.output('blob');
      return blob;
    }
    

    so that it is still a blob when passing to createMail() function. Now it works perfectly.