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

Extra newline added to the end of embedded message/rfc822 attachments when sending via SmtpClient #510

Closed
The-Nutty opened this issue Oct 2, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@The-Nutty
Copy link

Describe the bug
In my use case i am dkiming an email, then sending it using the SmtpClient, SMTP client appears to be adding an extra newline to the end of the message/rfc822 attachment in the email (see example) thus breaking the dkim signature. My understanding is that after calling message.Prepare the email body should not need to be modified further when sending (allowing you to dkim it).

To Reproduce
The following is a C# console app that once MailKit is installed should run fine

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

namespace Testing
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var message = MimeMessage.Load(new MemoryStream(Encoding.UTF8.GetBytes(@"From: [email protected]
Date: Fri, 15 Feb 2019 16:00:08 +0000
Subject: report_with_no_body
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status; boundary=""A41C7.838631588=_/mm1""


Processing your mail message caused the following errors:

error: err.nosuchuser: [email protected]

--A41C7.838631588=_/mm1
Content-Type: message/delivery-status

Reporting-MTA: dns; mm1
Arrival-Date: Mon, 29 Jul 1996 02:12:50 -0700

Final-Recipient: RFC822; [email protected]
Action: failed
Diagnostic-Code: X-LOCAL; 500 (err.nosuchuser)

--A41C7.838631588=_/mm1
Content-Type: message/rfc822

Received: from urchin.netscape.com ([198.95.250.59]) by mm1.sprynet.com with ESMTP id <148217-12799>; Mon, 29 Jul 1996 02:12:50 -0700
Received: from gruntle (gruntle.mcom.com [205.217.230.10]) by urchin.netscape.com (8.7.5/8.7.3) with SMTP id CAA24688 for <[email protected]>; Mon, 29 Jul 1996 02:04:53 -0700 (PDT)
Sender: [email protected]
Message-ID: <[email protected]>
Date: Mon, 29 Jul 1996 02:04:52 -0700
From: Jamie Zawinski <[email protected]>
Organization: Netscape Communications Corporation, Mozilla Division
X-Mailer: Mozilla 3.0b6 (X11; U; IRIX 5.3 IP22)
MIME-Version: 1.0
To: [email protected]
Subject: unsubscribe
References: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

unsubscribe
--A41C7.838631588=_/mm1--")));

            //prepare before dkim
            message.Prepare(EncodingConstraint.EightBit);
            // Im not actually going to dkim the email here as that should not make a difference to the result of this and i would need to pull in certs etc.. 

            // write the message to console, notice that after unsubscribe in the message/rfc822 part there is no new line.
            Console.WriteLine("Message after being prepared");
            using (var stream = new MemoryStream())
            {
                message.WriteTo(stream);
                stream.Position = 0;
                Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
            }

            // now send email

            using (var stream = new MemoryStream())
            {
                var client = new SmtpClient(new MailKit.ProtocolLogger(stream, true))
                {
                    SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
                    LocalDomain = "localhost"
                };
                await client.ConnectAsync(
                    "localhost", 25, SecureSocketOptions.Auto);
                await client.SendAsync(message, MailboxAddress.Parse("me@localhost"), new List<MailboxAddress>{MailboxAddress.Parse("me@localhost")});
                client.Disconnect(true);
                Console.WriteLine("SMTP logs");
                Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
            }


            Console.ReadLine();
        }
    }
}

Output of this program:

Message after being prepared
From: [email protected]
Date: Fri, 15 Feb 2019 16:00:08 +0000
Subject: report_with_no_body
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status; boundary="A41C7.838631588=_/mm1"


Processing your mail message caused the following errors:

error: err.nosuchuser: [email protected]

--A41C7.838631588=_/mm1
Content-Type: message/delivery-status

Reporting-MTA: dns; mm1
Arrival-Date: Mon, 29 Jul 1996 02:12:50 -0700

Final-Recipient: RFC822; [email protected]
Action: failed
Diagnostic-Code: X-LOCAL; 500 (err.nosuchuser)

--A41C7.838631588=_/mm1
Content-Type: message/rfc822

Received: from urchin.netscape.com ([198.95.250.59]) by mm1.sprynet.com with ESMTP id <148217-12799>; Mon, 29 Jul 1996 02:12:50 -0700
Received: from gruntle (gruntle.mcom.com [205.217.230.10]) by urchin.netscape.com (8.7.5/8.7.3) with SMTP id CAA24688 for <[email protected]>; Mon, 29 Jul 1996 02:04:53 -0700 (PDT)
Sender: [email protected]
Message-ID: <[email protected]>
Date: Mon, 29 Jul 1996 02:04:52 -0700
From: Jamie Zawinski <[email protected]>
Organization: Netscape Communications Corporation, Mozilla Division
X-Mailer: Mozilla 3.0b6 (X11; U; IRIX 5.3 IP22)
MIME-Version: 1.0
To: [email protected]
Subject: unsubscribe
References: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

unsubscribe
--A41C7.838631588=_/mm1--

SMTP logs

Connected to smtp://localhost:25/?starttls=when-available
S: 220 DESKTOP-6BUQHK5 ESMTP SubEthaSMTP null
C: EHLO localhost
S: 250-DESKTOP-6BUQHK5
S: 250-8BITMIME
S: 250-AUTH LOGIN
S: 250 Ok
C: MAIL FROM:<me@localhost>
S: 250 Ok
C: RCPT TO:<me@localhost>
S: 250 Ok
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
C: From: [email protected]
C: Date: Fri, 15 Feb 2019 16:00:08 +0000
C: Subject: report_with_no_body
C: To: [email protected]
C: MIME-Version: 1.0
C: Content-Type: multipart/report; report-type=delivery-status; boundary="A41C7.838631588=_/mm1"
C:
C:
C: Processing your mail message caused the following errors:
C:
C: error: err.nosuchuser: [email protected]
C:
C: --A41C7.838631588=_/mm1
C: Content-Type: message/delivery-status
C:
C: Reporting-MTA: dns; mm1
C: Arrival-Date: Mon, 29 Jul 1996 02:12:50 -0700
C:
C: Final-Recipient: RFC822; [email protected]
C: Action: failed
C: Diagnostic-Code: X-LOCAL; 500 (err.nosuchuser)
C:
C: --A41C7.838631588=_/mm1
C: Content-Type: message/rfc822
C:
C: Received: from urchin.netscape.com ([198.95.250.59]) by mm1.sprynet.com with ESMTP id <148217-12799>; Mon, 29 Jul 1996 02:12:50 -0700
C: Received: from gruntle (gruntle.mcom.com [205.217.230.10]) by urchin.netscape.com (8.7.5/8.7.3) with SMTP id CAA24688 for <[email protected]>; Mon, 29 Jul 1996 02:04:53 -0700 (PDT)
C: Sender: [email protected]
C: Message-ID: <[email protected]>
C: Date: Mon, 29 Jul 1996 02:04:52 -0700
C: From: Jamie Zawinski <[email protected]>
C: Organization: Netscape Communications Corporation, Mozilla Division
C: X-Mailer: Mozilla 3.0b6 (X11; U; IRIX 5.3 IP22)
C: MIME-Version: 1.0
C: To: [email protected]
C: Subject: unsubscribe
C: References: <[email protected]>
C: Content-Type: text/plain; charset=us-ascii
C: Content-Transfer-Encoding: 7bit
C:
C: unsubscribe
C:
C: --A41C7.838631588=_/mm1--
C: .
S: 250 Ok
C: QUIT
S: 221 Bye

When running this you will see when the message is written our hte first time after preparing it there is no new line after unsubscribe (in the message/rfc822 part). However when sending it there is a new line after unsubscribe in the SMTP logs.
Expected behavior
There should be no difference in the email body between prepaeiring it as otherwise any attempt at dkiming is useless as when the next party received the email the dkim body signature will not match.

Desktop (please complete the following information):

  • OS: Windows 10
  • dotnet version: dotnet core V2.2

Additional context
I only have reproduce the issue with this sample attached above but i have seen similar symptoms in emails with other attachments as well.

@jstedfast jstedfast changed the title SmtpClient Adding extra newline to embedded attachments upon saving. SmtpClient adding an extra newline to the end of embedded message/rfc822 attachments when sending. Oct 2, 2019
@jstedfast jstedfast changed the title SmtpClient adding an extra newline to the end of embedded message/rfc822 attachments when sending. Extra newline added to the end of embedded message/rfc822 attachments when sending via SmtpClient Oct 2, 2019
@jstedfast jstedfast transferred this issue from jstedfast/MailKit Oct 2, 2019
jstedfast added a commit that referenced this issue Oct 2, 2019
Also fixed DKIM and ARC signing to always enable FormatOptions.EnsureNewLine.

Fixes issue #510
@jstedfast jstedfast added the bug Something isn't working label Oct 2, 2019
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