2

I am creating 2 desktop applications in c # with netframework 4.7. In one application I am sending mail by smtp to the outlook server of 365, and in another I am reading the mail through imap.

I'm using @jstedfast 's MailKit library and I'm following this answer as I'm interested in my application handling the delivery status of the mails. The problem I have is that in the delivery status emails I am not receiving the Original-Envelope-Id header.

So what could I be doing wrong? Is it a missing configuration on the outlook server? Is it a missing configuration in the library?

Currently my classes are like this:

The classes and methods that send the mail:

public class EnvioDeCorreo {
    private const string Usuario = "miusuario@outlook.com";
    private const string Password = "mipass";
    private const string Host = "smtp.office365.com";

    public async Task Enviar() {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("PrFq", Usuario));
        message.To.Add(new MailboxAddress("usu1", "usu1@gmail.com"));
        message.To.Add(new MailboxAddress("notvalid", "notvalid.notvalid@notexist.com"));
        // Adding others mailbox's

        message.Subject = "Hola de envio desde c#";
        message.Body = new TextPart("plain") {
            Text = "Hola desde c#"
        };

        using (var client = new DSNSmtpClient(new ProtocolLogger("DSNSmtpClient.log"))) {
            client.MessageSent += this.OnMessageSent;
            await client.ConnectAsync(Host, 587, SecureSocketOptions.StartTls);
            await client.AuthenticateAsync(Usuario, Password);

            if (client.Capabilities.HasFlag(SmtpCapabilities.Dsn))
                Debug.WriteLine("Soporta DSN");
            else return;

            await client.SendAsync(message);
            await client.DisconnectAsync(true);
        }

    }

    private void OnMessageSent(object sender, MessageSentEventArgs e)
        => Debug.WriteLine($"En MessageSent {e.Response}");
}

public class DSNSmtpClient : SmtpClient {
    public DSNSmtpClient(ProtocolLogger protocolLogger) : base(protocolLogger) { }
    protected override string GetEnvelopeId(MimeMessage message) => message.MessageId;
    protected override DeliveryStatusNotification? GetDeliveryStatusNotifications(MimeMessage message, MailboxAddress mailbox)
        => DeliveryStatusNotification.Failure | DeliveryStatusNotification.Success;

The class that consults the emails:

public class ManejoDeEstadoDeCorreo {
    private const string Usuario = "miusuario@outlook.com";
    private const string Password = "mipass";
    private const string Host = "outlook.office365.com";

    public void Manejar() {
        using (var client = new ImapClient(new ProtocolLogger("ImapClient.log"))) {
            client.Connect(Host, 993, true);
            client.Authenticate(Usuario, Password);

            var inboxFolder = client.Inbox;
            inboxFolder.Open(FolderAccess.ReadOnly);
            var query = SearchQuery.SubjectContains("Hola de envio desde c#");
            var uids = inboxFolder.Search(query);
            foreach (var uid in uids) {
                var message = inboxFolder.GetMessage(uid);
                this.ValidarEstado(message);
            }

            client.Disconnect(true);
        }
    }

    private void ValidarEstado(MimeMessage message) {
        var report = message.Body as MultipartReport;
        if (report != null && report.ReportType != null 
            && report.ReportType.Equals ("delivery-status", StringComparison.OrdinalIgnoreCase)) {
            foreach (var mds in report.OfType<MessageDeliveryStatus>()) {
                var statusGroup = mds.StatusGroups[0];
                var envId = statusGroup["Original-Envelope-Id"];
                if (envId is null) {
                    // It is here where the header is null.
                    Debug.WriteLine("Not working");
                } else {

                }
            }
        }
    }
}

<UPDATE#1>

The internet headers in the rebounded mail are:

Received: from CY1NAM02HT029.eop-nam02.prod.protection.outlook.com
 (2603:10b6:5:3b5::33) by DM5PR0201MB3527.namprd02.prod.outlook.com with HTTPS
 via DS7PR03CA0058.NAMPRD03.PROD.OUTLOOK.COM; Thu, 11 Feb 2021 15:16:02 +0000
ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass;
 b=Z+DIYDWWx5OuB0Tgm7XrHZBTPvvmILCi3E6Juwc43NruUR5kCvdUIWGfBsiTW8uAcNbFC3Qm03Lqj/hVUxCxWs+RpkdS9CLeemdH1iELXR8COrdY4vAA/kPzWQ7BvD1ZSHZfTv5y+FY1QyMAWk1Qi2Qg+7kTBnXvAM8TIBGdOrq+iZsN/Nvu1kIAv0yVRw7b7BHaWvGKByRAm0NPXPKUIGIByEAK99VfncRoH0vjaXKF/8MhJii9o7cmr/C6eO45//BdIs8FrkhmSubJ1zruoWKi3VfWnbnxkaerMg0faa8hBg6WEE/cuNp4wQDf29DO/t76YEeGaMPvwdiQlLja4g==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=bntYEMMAxsEqgCcoJ8/KQp1p8eLqi9xVUMW+EQarFKg=;
 b=hHq6ACZA3T7XMIyDBZBWvY+qNHyM8kYfzq5bP0essCNAES2SduY6Pf8mHklVyDQ0d9iD1Mpem8tg3ssIc3b0OFE1aLqN/Bl1She/tgBXpsrSn/gjhHzzKBKmCLF/sw5sH0MhAQfithInW1izqoD1KCp5+PNWWiGWHDpn5FBvxq3MVhRSxqlIfv8Qwg/SrrWtnKwz03UmCHJxq8V0xfMD/xs70yGKofJCqaft4K0bAgQ2fkUtBAElGJVR71HUhEsPd+sMr5PNWzF0hZO8U1YamQlIzo1njEiHfO1Lz0IgzKxJ/UWHtgfkVzkjQWSs/law5nsmGQ6CCAK1xSuqQQGwGQ==
ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is
 40.92.21.103) smtp.rcpttodomain=outlook.com
 smtp.helo=nam12-bn8-obe.outbound.protection.outlook.com; dmarc=pass (p=none
 sp=quarantine pct=100) action=none header.from=outlook.com; dkim=none
 (message not signed); arc=pass (0 oda=0 ltdi=1)
Received: from CY1NAM02FT053.eop-nam02.prod.protection.outlook.com
 (10.152.74.51) by CY1NAM02HT029.eop-nam02.prod.protection.outlook.com
 (10.152.75.100) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3846.25; Thu, 11 Feb
 2021 15:16:01 +0000
Authentication-Results: spf=pass (sender IP is 40.92.21.103)
 smtp.helo=NAM12-BN8-obe.outbound.protection.outlook.com; outlook.com;
 dkim=none (message not signed) header.d=none;outlook.com; dmarc=pass
 action=none header.from=outlook.com;compauth=pass reason=100
Received-SPF: Pass (protection.outlook.com: domain of
 NAM12-BN8-obe.outbound.protection.outlook.com designates 40.92.21.103 as
 permitted sender) receiver=protection.outlook.com; client-ip=40.92.21.103;
 helo=NAM12-BN8-obe.outbound.protection.outlook.com;
Received: from NAM12-BN8-obe.outbound.protection.outlook.com (40.92.21.103) by
 CY1NAM02FT053.mail.protection.outlook.com (10.152.74.165) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.3846.25 via Frontend Transport; Thu, 11 Feb 2021 15:16:01 +0000
X-IncomingTopHeaderMarker:
 OriginalChecksum:244D9E6BC8291DDF7864888CCA2522564A1C19DE8A7F27715FAF27887E37E532;UpperCasedChecksum:F0829C4E3FEAB9CC01A8A79EC1D2E0F80219EB3B9B12FB967A7C4D7F10CA7EA1;SizeAsReceived:2800;Count:26
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=UYr3N3ihnvjrRXgTdOaSvzhYvfUD7Rl5uivX0xXFAEhSB5mD+RgXvGCNdy6FP4HyQ8of8siD2IBpZdZuE9+SE9f1UlGkydtSocXYdB5PvinIFOXm9CgGoCO8n8DSf6ozXtOOl6pg2wvniWHWU0tOeHqy5iFMckLN8T6slEOjLF+Q+d+52BcfIi3GE4YxcIve7LkfsT2MAiLqwYE+psE7HdR3Tqp8m0k9KAS/FDN58Mde5hXgcqZodWNCLOU4UjOtauDriXABaj4DfkcAD70+Rv7YsI4W4+sYlSm7mukFlESR9CLmN386LKwMc7ShnOKeLjAB5R7H4MjP/IJm3uM7RA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=bntYEMMAxsEqgCcoJ8/KQp1p8eLqi9xVUMW+EQarFKg=;
 b=mB8rKCmo3w2ORP45sAF6W9mVK+3tD6w94Tzk3drve5eSWtRfq3beNW3jGxmOZgSaaJ2MefMP5NDdlQBPDhY8POPWZLI/CVyq3eOmDPGuIb9UR1BM/yiM/beFgCzNyf+JrCS2QPhbHuqDlyA1hdW69YesEYthQCwg5oDoQzK8CWICD80/WL1GQKdox/OwWUSaBJm9yuB/0xd76b/5+IiKMw6sQ8NvMN/hq/lzwc4WbqwLnFJ5tW6pHI9N1ZvuKS5WqpoiRyZ0eHKGEvEO41Kld5gDmCDn4+maJmEJioPI2KlZVPUdPi/r/5XbKmOwTORHLuqpQq0qET9nT6mGWOqhmQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
 dkim=none; arc=none
MIME-Version: 1.0
From: Microsoft Outlook <postmaster@outlook.com>
To: <marlonedu.pe@outlook.com>
Date: Thu, 11 Feb 2021 15:16:00 +0000
Content-Type: multipart/report; report-type=delivery-status;
    boundary="05ae0700-285b-45c4-908a-3d9dddcde4a8"
X-MS-Exchange-Message-Is-Ndr:
Content-Language: en-US
Message-ID:
 <7edeb0e6-fbcb-49ae-b634-91891d033da4@BN8NAM12HT208.mail.protection.outlook.com>
In-Reply-To:
 <DM5PR0201MB3527F7323E6CA54BFFA40BC1808C9@DM5PR0201MB3527.namprd02.prod.outlook.com>
References:
 <DM5PR0201MB3527F7323E6CA54BFFA40BC1808C9@DM5PR0201MB3527.namprd02.prod.outlook.com>
Subject: Undeliverable: Hola de envio desde c#
Auto-Submitted: auto-replied
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: BN8NAM12HT208:|CY1NAM02HT029:
X-Microsoft-Antispam-Untrusted: BCL:0;
X-Microsoft-Antispam-Message-Info-Original:
 b2FX5Q7X8mEsxo7S7vPeWKs3zuy2rb3PBfBQElcyLHCKjDjLTbDXD4LjnBy/+2DwyZCrDgxZPO+ORYNvH+u69Nk7Y4FfOVXrRaIBf53+baab+riNHJ0CM6CuxVXRewdwXQA5FWjaYskVjXDKobwXYKyMCQmfwwtFrQ2UxI5XK1ujrEcJc5FmntGUlKCYOicyRYaOyfi8R1PsQZXeadRNmWP0KlqWp5Q0tOMDRgO0IiJHge3Lj8fkwl+T9Yv98UTN6cX9kmaT8HoxFL/bZMyZ4EIDZoXygOMdEWR+CdD04oHVgxQxL4ROaTQqbUs9VH5sxs+YjXnz8tHSXRI+eC0qlfNyDUllyKKObSV+oFOVbk1g3teXTyPEK/brG/5mP94oAlL7Dq2aHkkDUWXo7nxueQ==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8NAM12HT208
X-IncomingHeaderCount: 26
Return-Path: <>
X-MS-Exchange-Organization-ExpirationStartTime: 11 Feb 2021 15:16:01.5895
 (UTC)
X-MS-Exchange-Organization-ExpirationStartTimeReason: OriginalSubmit
X-MS-Exchange-Organization-ExpirationInterval: 1:00:00:00.0000000
X-MS-Exchange-Organization-ExpirationIntervalReason: OriginalSubmit
X-MS-Exchange-Organization-Network-Message-Id:
 af2de7d2-b43d-44c9-41ea-08d8ce9ff145
X-EOPAttributedMessage: 0
X-EOPTenantAttributedMessage: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa:0
X-MS-Exchange-Organization-MessageDirectionality: Incoming
X-MS-Exchange-Transport-CrossTenantHeadersStripped:
 CY1NAM02FT053.eop-nam02.prod.protection.outlook.com
X-MS-Exchange-Transport-CrossTenantHeadersPromoted:
 CY1NAM02FT053.eop-nam02.prod.protection.outlook.com
X-MS-Exchange-Organization-AuthSource:
 CY1NAM02FT053.eop-nam02.prod.protection.outlook.com
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-UserLastLogonTime: 2/11/2021 3:03:56 PM
X-MS-Office365-Filtering-Correlation-Id: af2de7d2-b43d-44c9-41ea-08d8ce9ff145
X-MS-Exchange-EOPDirect: true
X-Sender-IP: 40.92.21.103
X-SID-PRA: POSTMASTER@OUTLOOK.COM
X-SID-Result: PASS
X-MS-Exchange-Organization-PCL: 2
X-MS-Exchange-AtpMessageProperties: SA|SL
X-Microsoft-Antispam: BCL:0;
X-OriginatorOrg: outlook.com
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Feb 2021 15:16:01.5225
 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: af2de7d2-b43d-44c9-41ea-08d8ce9ff145
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-AuthSource:
 CY1NAM02FT053.eop-nam02.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: Internet
X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1NAM02HT029
X-MS-Exchange-Transport-EndToEndLatency: 00:00:00.7358426
X-MS-Exchange-Processed-By-BccFoldering: 15.20.3846.029
X-MS-Exchange-Organization-SCL: -1
X-Message-Info:
    5vMbyqxGkddr6qryKXlymvxSs2BogX+pwinZF3Q51/6TAEpZ2GEXqF4cPOZNtds9cm3nXVJ27DCXga90+ZjC+n44nQPk0MDdzcobW4SSMY9YB33FUo1l/+kbe71TCU4kQ5RYlJ61GDI3bKdw3MEUohfVWAYinrK+Reko9XxmeaPOJBI77Z3vGMGoDrDKC6gX1a695SHyMFQxBJFYFWGEcg==
X-Message-Delivery: Vj0xLjE7dXM9MDtsPTA7YT0xO0Q9MTtHRD0xO1NDTD0tMQ==
X-Microsoft-Antispam-Mailbox-Delivery:
    abwl:0;wl:0;pcwl:0;kl:0;iwl:0;dwl:0;dkl:0;rwl:0;ucf:0;jmr:0;ex:0;auth:1;dest:I;OFR:SpamFilterPass;ENG:(5062000282)(90000117)(90012020)(91020020)(91040095)(9000001)(9050020)(9065021)(9100273)(5061607266)(5061608174)(1001270)(4900115)(2008001094)(2008000189)(4920090)(6215004)(4950131)(4990090)(9110004);
X-Microsoft-Antispam-Message-Info:
    =?us-ascii?Q?usubTJWeTy0AKtIVV2ADLN+pwmNvYZKKHkqq5PoU2KFy6ISj7vmTi2vA4er6?=
 =?us-ascii?Q?zMYX3E/2TrDf1ImQT7VfGQEtk+8fc+bfJXze4PFIxD0qI0y4Xn54WV14VAJX?=
 =?us-ascii?Q?ahyy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2ZReULCTUmkTPIr/GadCV+3vi+KCJJx1aiLVcNyxy?=
 =?us-ascii?Q?UH0O5DriKbBA2ldyM27BWBQkNUKxcR4pKQaJmZ/bAldiKf3WZV8eA5Ot4orV?=
 =?us-ascii?Q?cP18nq9eDscKLMA9lNQYnqtrYXa4oTyewMIxHAGqZ7+y4ptqqnqZHmL7Lsir?=
 =?us-ascii?Q?Z8OlcdzHmOaHag5DfJdTCRlzEKNxJ8WFh/VkZ0DfcpcAF0yrrObk53j0UJ6q?=
 =?us-ascii?Q?EczB+X/DSo7Tn85c8tjiZUCtVHLwAW+als72/D3uD/jn8IevPVH0g1M/Ng3U?=
 =?us-ascii?Q?kZgK4aVhKgp0RH8OydIGzO0ysqjfENzrfrS8BWNWQ+wnSPHEzLcwt8KlKlJU?=
 =?us-ascii?Q?9ZXHDzrhZnpafKNSkoH2nP0hyRvWp45Qx7D5mPiqlKYbIeQqFoF8BvfpYC03?=
 =?us-ascii?Q?koBoTbgAZtvKhd+LDhqRFZqPrxZmZtJdEamYKUS0S3ncaXWGSg=3D=3D?=
X-EsetId: 37303A292182D265617163

</UPDATE#1>

<UPDATE#2>

Also, this is the information from the mds.StatusGroups at debug mode: enter image description here </UPDATE#2>

Marlonchosky
  • 494
  • 5
  • 16
  • What status headers does it have? – jstedfast Feb 12 '21 at 19:27
  • @jstedfast I updated the question adding the internet headers of the rebounded mail – Marlonchosky Feb 12 '21 at 23:01
  • @jstedfast Also, I added the information founded in mds.StatusGroups – Marlonchosky Feb 12 '21 at 23:22
  • Is it possible your mail server does not fully adhere to DSN? Do you receive a Original-Envelope-Id if you use a different mail server? – TheFunk Feb 19 '21 at 16:24
  • @TheFunk interesting, nevertheless, I validate the DSN capabilites with client.Capabilities.HasFlag(SmtpCapabilities.Dsn) – Marlonchosky Feb 19 '21 at 18:09
  • 1
    I *may* have stumbled on a reason for this... will try to have a solution in an upcoming v3.0 that I'm currently working on. – jstedfast Sep 14 '21 at 15:11
  • @jstedfast thank you for your support. I'm going to try it when the new version is released – Marlonchosky Sep 14 '21 at 15:26
  • 1
    MailKit was not properly hex-encoding the `ENVID` parameter for the `MAIL FROM` command and so if `GetEnvelopeId()` returned a string with `+`, `=`, or characters outside of the range 33-126 (inclusive), then it's possible that the SMTP server was ignoring it. – jstedfast Sep 14 '21 at 16:48
  • @jstedfast interesting. In the new release, Mailkit will be properly hex-encoding for the cases that you are commenting, right? – Marlonchosky Sep 14 '21 at 17:16
  • 1
    MailKit v3.0 will be correctly encoding them, correct. – jstedfast Sep 14 '21 at 17:17
  • @jstedfast perfect. If you need some testing for the new version of Mailkit, I can test the problem/feature I asked in this post before the new release is published. I have some time on weekends, so feel free to contact me if you want help. – Marlonchosky Sep 14 '21 at 18:32

0 Answers0