Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some (PDF-)Files are not readable after sending via MimeKit #598

Closed
migeold opened this issue Aug 25, 2020 · 6 comments
Closed

Some (PDF-)Files are not readable after sending via MimeKit #598

migeold opened this issue Aug 25, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@migeold
Copy link

migeold commented Aug 25, 2020

Hello,

I observe a strange behavior when sending PDF files via MimeKit.
Most files are transmitted without problems, but few are not readable.
It seems, that Lf is converted into CrLf.
Could this be caused by MimeKit?

OS: Windows 10 2004 (19041.450) 64-Bit
.Net 4.8, WinForms
MimeKit Version: 2.9.0.0
Tested with Email-Provider: gmail.com

I attached 2 files: 'Original.pdf' before and 'Corrupt.pdf' after sending.
Screenshot 'Difference.png' shows the difference.
'Original.pdf' is readable with 'Adobe Acrobat Reader DC 2020.012.20043', 'Corrupt.pdf' not.

Attachment is added with this code:
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = htmlBody;
bodyBuilder.Attachments.Add(path);
mimeMessage.Body = bodyBuilder.ToMessageBody();

Thank you very much for your help!
Michael.

Original.pdf
Corrupt.pdf
Difference

@jstedfast
Copy link
Owner

Try doing this:

var attachment = (MImePart) bodyBuilder.Attachments.Add(path);
attachment.ContentTransferEncoding = ContentEncoding.Base64;

@migeold
Copy link
Author

migeold commented Aug 25, 2020

Thank you very much!
Setting the Encoding correctly solved my problem.

@migeold migeold closed this as completed Aug 25, 2020
@jstedfast
Copy link
Owner

Awesome!

@jstedfast
Copy link
Owner

I've modified the BodyBuilder logic to always use the base64 encoding for non-text attachments.

The problem was that for your particular PDF, since it did not contain any nul-bytes or more than ~17% 8bit bytes, the BodyBuilder chose to encode the PDF using quoted-printable which is more efficient size-wise than base64 for your PDF, but the problem is that it's not a great encoding for non-text attachments because of the newline issue you hit.

@jstedfast jstedfast added the bug Something isn't working label Aug 26, 2020
@migeold
Copy link
Author

migeold commented Aug 27, 2020

Thanks a lot!
Your update will also help other users with similar problems.
I'm looking forward to the new release.
What do you recommend for attachments in general?
With new version, should we use bodyBuilder.Attachments.Add(path); only? Is your helpful workaround (attachment.ContentTransferEncoding = ContentEncoding.Base64;) obsolete now?

@jstedfast
Copy link
Owner

When I make a new release, you won't need to specify base64. It'll do it for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants