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

fletch413 96 Reputation points
2021-10-14T14:56:36.897+00:00

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:

Back End:
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?

Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. fletch413 96 Reputation points
    2021-10-19T14:03:38.127+00:00

    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.

    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);
          });
      }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.