Search code examples
c#outlooksmtpgmailmimekit

Emails sent with MimeKit / Outlook are falling to gmail's spam folder for the first time


We are using MimeKit as SMTP client for sending emails to our users. Typically, the first time it arrives to their gmail accounts, it falls into the spam box. Then if removed the 'spam' flag, subsequent emails stay in the inbox.

Is there something necessary to do at C# level? or maybe at Outlook/Azure-DNS level?

The C# code snippet is as follows:

            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Notifier", smtpUser));
            message.To.Add(new MailboxAddress("User", recipient));
            message.Subject = subject;

            var bodyBuilder = new BodyBuilder() { HtmlBody = html, TextBody = text };
            message.Body = bodyBuilder.ToMessageBody();

                using (var client = new SmtpClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    client.Connect(current.SmtpUrl, current.SmtpPort, SecureSocketOptions.StartTls);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    client.Authenticate(current.SmtpUser, current.SmtpPass);
                    client.Send(message);
                    client.Disconnect(true);
                }   

For the first time, gmail is showing it flagged as 'spam': enter image description here

Pressing 'Looks safe' will make subsequent emails valid, but we need the first one to look safe.

I am attaching also the entire mail content received at gmail:

Delivered-To: [email protected]
Received: by 2002:a0c:f08c:0:0:0:0:0 with SMTP id g12csp672696qvk;
        Wed, 4 Sep 2019 07:16:38 -0700 (PDT)
X-Received: by 2002:a9d:3b77:: with SMTP id z110mr8444689otb.93.1567606358405;
        Wed, 04 Sep 2019 07:12:38 -0700 (PDT)
X-Google-Smtp-Source: APXvYqw3m+1ZsX0fhLp8SuYRXSxfxUtUaYyLo4BZjxq+nVuni9VUHkVmOBnhBg/vThsIaIoA3chE
X-Received: by 2002:a9d:3b77:: with SMTP id z110mr8444615otb.93.1567606357487;
        Wed, 04 Sep 2019 07:12:37 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1567606357; cv=pass;
        d=google.com; s=arc-20160816;
        b=MvKIcQqivTBGk3GqNz9MiIMtzUt4gJc0hJMoVHp7LMJyHO+AAiPfDGAdQYy7MwF0YF
         QRm1vkQL5f2DMC3ukgYsZldfk6A4xwbFqkqSQOtZCqQ7zHk3rusUoTIaO6a2fDn6nIKZ
         cUY+92Y0+hOpu0MF/bGYCfy+3+ncJxuOVLcgnWuafrlvyQioviQ0LEsiQT4MsoOCu1gd
         iuCDLz6u+rQ65Rti/FoyHePAaxvQzzjiTFRqXimu4z141Khpbd6x/kzrac5ko7M01DcT
         jffxtMeD0h+OPBmATBAS9uAo7RCjD5LQXSUrtbYy7PJTbYmakc82O2Xk3r1qSwIGWcpi
         aOkw==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=mime-version:content-language:accept-language:message-id:date
         :thread-index:thread-topic:subject:to:from:dkim-signature;
        bh=LjkQ2fZg1ziyuIJx9jwPMT7LT3T4ABbJmFUMJxavtNY=;
        b=NHWmx+VtSxm9xrCGtEHdQPhCs7cF8qepGyU/uZ2MDF+dLBQygpghVzD4zwondLS0En
         CpV/xfSbzx5aRLTnEq3468awqMnPRTmxjT3X/m3VLcq8sSg43ZtJLr5dxlUX2Eyt5Chu
         v4bnC+0wQM05OCMQkTayCWEE/LL8aqKcJYDuK6wd4vU2cSBUZzieDXczf/ysnodrhWgx
         vgvlaa4To4rWwbBe6N2C9lSa3xOO8jU+6f4yS1ttThDPljvd/wR7VTHAU5oF4EH/UQ3e
         wKWdfWv9PPR/Espq66dONAgaqAcauPO35YYhMMu4ND2TCbDqmF7nPoZkf/iitGvYgL78
         1Xvw==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass [email protected] header.s=selector2-efferenthealthllc-onmicrosoft-com header.b=ETAIKEtN;
       arc=pass (i=1 spf=pass spfdomain=efferenthealth.com dkim=pass dkdomain=efferenthealth.com dmarc=pass fromdomain=efferenthealth.com);
       spf=pass (google.com: domain of [email protected] designates 2a01:111:f400:fe44::629 as permitted sender) [email protected]
Return-Path: <[email protected]>
Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on0629.outbound.protection.outlook.com. [2a01:111:f400:fe44::629])
        by mx.google.com with ESMTPS id k12si10006003oiw.30.2019.09.04.07.12.37
        for <[email protected]>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
        Wed, 04 Sep 2019 07:12:37 -0700 (PDT)
Received-SPF: pass (google.com: domain of [email protected] designates 2a01:111:f400:fe44::629 as permitted sender) client-ip=2a01:111:f400:fe44::629;
Authentication-Results: mx.google.com;
       dkim=pass [email protected] header.s=selector2-efferenthealthllc-onmicrosoft-com header.b=ETAIKEtN;
       arc=pass (i=1 spf=pass spfdomain=efferenthealth.com dkim=pass dkdomain=efferenthealth.com dmarc=pass fromdomain=efferenthealth.com);
       spf=pass (google.com: domain of [email protected] designates 2a01:111:f400:fe44::629 as permitted sender) [email protected]
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NNjokEVhuXR5/dOYj6NjeePpEmQbWt5XFctGf5IRGJuGMHAuAd9cXjCTkqduSGP3bTLRqIYjwDRthcj10zcZVudxgjhe6OB50s8KJAMpXVdh+HvGD1JoqUq2BW9M0qyOQ0YDl6Fq6F3DAWMpWNIZRPlrlaOkNaO8ifHj1okiSrThxzIxuGkzGx5iUt8/S7LOBuvzgkVWoT0NendRzvl/mB1ZQgYWcpBa+PulPlYgCTtktO1rbYZgKr5qFvJGbgBvQ1bnyZtHmZRQrA7mNegfeXrK544dGEcXs1DcFo+vNeNR53dvBWFyPCkmPpdl4+rKGRkfo/PY3B1M+D003HXuqQ==
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=LjkQ2fZg1ziyuIJx9jwPMT7LT3T4ABbJmFUMJxavtNY=; b=G0nSGcuGCOFD8gcKNWVcBAZ2EIGwicBs0yITe0iyVc+3BIm2/QUfSzcPk0+FRIKtklfUru/c/sTaag3j/6RwPp98uj+2I8k/hdLY0+72qvx595t+E+CMXSGyLmBxHYEcgIzWuckwnRdnUaeU0LwLsqKiCLV1QakRniWXnlzdKjvAXozRlHfnx/AOmyGGYUc5u1zTM5VJWmxprCL+bqEOZi1agY4A94MJK5UyW+YEOEgNEJuyx1+pgdpeQkYVqeYVAk5AcFKLgYNBLpxvW0oVqRMECOV6zlI8xJU8qiFaW64qrBQVYQF8nRfQZd8oI3nmIIoPrstNWqgEOH/UEajoNA==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=efferenthealth.com; dmarc=pass action=none header.from=efferenthealth.com; dkim=pass header.d=efferenthealth.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efferenthealthllc.onmicrosoft.com; s=selector2-efferenthealthllc-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LjkQ2fZg1ziyuIJx9jwPMT7LT3T4ABbJmFUMJxavtNY=; b=ETAIKEtNCHLfnl7Fzp8XUI+NlLP/HVTzkmlFS6XwZsLZP5vOj0hJ+1KASa6mjEAvPJbmM2iDTvCMRW8UfigfVSC6hT04OyaiGjNzOjTkR8gN1TCvzNr7esQzyXR94vmz2MjLiycuhNJJMdGGRUoYZUxGhvj6F0muEBoZZ034GLQ=
Received: from BN6PR1401MB2004.namprd14.prod.outlook.com (10.174.116.149) by BN6PR1401MB1907.namprd14.prod.outlook.com (10.174.115.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2220.18; Wed, 4 Sep 2019 14:12:35 +0000
Received: from BN6PR1401MB2004.namprd14.prod.outlook.com ([fe80::1de9:93da:5da7:4415]) by BN6PR1401MB2004.namprd14.prod.outlook.com ([fe80::1de9:93da:5da7:4415%11]) with mapi id 15.20.2220.022; Wed, 4 Sep 2019 14:12:35 +0000
From: Notifier <[email protected]>
To: User <[email protected]>
Subject: Notification
Thread-Topic: Notification
Thread-Index: AQHVYyrMLhjkfFeVAkmoUM8Y198VPw==
Date: Wed, 4 Sep 2019 14:12:34 +0000
Message-ID: <[email protected]>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-clientproxiedby: SN4PR0401CA0034.namprd04.prod.outlook.com (2603:10b6:803:2a::20) To BN6PR1401MB2004.namprd14.prod.outlook.com (2603:10b6:405:59::21)
authentication-results: spf=none (sender IP is ) [email protected];
x-ms-exchange-messagesentrepresentingtype: 1
x-originating-ip: [13.84.163.72]
x-ms-publictraffictype: Email
x-ms-office365-filtering-correlation-id: 6291f624-e9b1-484f-f114-08d73141eea5
x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(7021145)(8989299)(4534185)(7022145)(4603075)(4627221)(201702281549075)(8990200)(7048125)(7024125)(7027125)(7023125)(5600166)(711020)(4605104)(1401327)(2017052603328)(7193020);SRVR:BN6PR1401MB1907;
x-ms-traffictypediagnostic: BN6PR1401MB1907:
x-ms-exchange-purlcount: 1
x-microsoft-antispam-prvs: <BN6PR1401MB1907D7529E3B99DE63CB070DA0B80@BN6PR1401MB1907.namprd14.prod.outlook.com>
x-ms-oob-tlc-oobclassifiers: OLM:660;
x-forefront-prvs: 0150F3F97D
x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(4636009)(366004)(136003)(39830400003)(396003)(42606007)(346002)(376002)(199004)(189003)(66946007)(2906002)(3480700005)(558084003)(476003)(316002)(25786009)(486006)(256004)(508600001)(52116002)(99286004)(66556008)(7116003)(66476007)(33656002)(305945005)(71200400001)(71190400001)(66446008)(64756008)(8936002)(6486002)(7736002)(81156014)(81166006)(8676002)(6306002)(51815007)(10126004)(386003)(6506007)(53936002)(66066001)(86362001)(221733001)(9686003)(236005)(6512007)(606006)(14454004)(6916009)(6436002)(186003)(54896002)(26005)(6116002)(102836004)(3846002)(1411001)(5660300002);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR1401MB1907;H:BN6PR1401MB2004.namprd14.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:3;MX:1;
received-spf: None (protection.outlook.com: efferenthealth.com does not designate permitted sender hosts)
x-ms-exchange-senderadcheck: 1
x-microsoft-antispam-message-info: bHkvE3Co8At1/X9BzBqbqZFNCIBAppNhqQqCJDoMByjqBv9VGO25sOpCKS8OSh1obZBR1t5nvkdn0CFuuFoGjhwNaXt7MakSQfeS8auVa0d/GUpLb7Y3oJ8Xm2az23Z4+l3YP4ITa5Fhd8DxliNV8JO4ZYDxC6uG3Gh8my4MewFPQScRgUgx49un88gu8zK3MDyUhxIlYB1WjdqPPG3xtQMIBHbA4EuPFa5Is2Jk7D4PsMjrpTvL8GyZVYhgVWC71OrwG7C575IB1XIanF7woc0VZxBlAjh1xY8gaUDYuT7Lc1b/cSKcyUjOG7J0XR8ilS4fp86IaGeKdivi4h2MJ8oqW+A9obH2+GW5NTpBMgvYE7zFNBQhxuuC/jxny7F2zs4z5vacC0HOXeUwgfOLWZsUdZC2SDW6HjXkw0TejIA=
x-ms-exchange-transport-forked: True
Content-Type: multipart/alternative; boundary="_000_98UUW7B0H8U4EB3P8CQCSUEN2localhostlocaldomain_"
MIME-Version: 1.0
X-OriginatorOrg: efferenthealth.com
X-MS-Exchange-CrossTenant-Network-Message-Id: 6291f624-e9b1-484f-f114-08d73141eea5
X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Sep 2019 14:12:34.9389 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: 02ecab7f-85de-4d13-9c86-db5eefd90ee2
X-MS-Exchange-CrossTenant-mailboxtype: HOSTED
X-MS-Exchange-CrossTenant-userprincipalname: zj/y6ItrIybuyjEREqOxS6sBThdrcjGbcWxxKICSNnb1h2GZnVz0UsJO0Kcp51LYMrFzeRKKgbFFnJPNgXa2GoLY5CBVrkaMzJbHf0oywnA=
X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR1401MB1907

--_000_98UUW7B0H8U4EB3P8CQCSUEN2localhostlocaldomain_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

VGhlIHVzZXIgQSBib3N0b25tZWRpY2FsZ3JvdXAgZnJvbSBCb3N0b24gTWVkaWNhbCBHcm91cCBo
YXMgc2hhcmVkIHdpdGggeW91IGEgSW1hZ2luZ1N0dWR5IHJlY29yZC4gWW91IGNhbiByZXZpZXcg
c3dvcmQ9YTM4YWEyMWQtMTJhMy04YTQ1LTZjNTEtMzUwZDNjMzY5ZDcwPg0K
--_000_98UUW7B0H8U4EB3P8CQCSUEN2localhostlocaldomain_
Content-Type: text/html; charset="utf-8"
Content-ID: <[email protected]>
Content-Transfer-Encoding: base64

PGh0bWw+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0i
dGV4dC9odG1sOyBjaGFyc2V0PXV0Zi04Ij4NCjwvaGVhZD4NCjxib2R5Pg0KVGhlIHVzZXIgQSBi
b3N0b25tZWRpY2FsZ3JvdXAgZnJvbSBCb3N0b24gTWVkaWNhbCBHcm91cCBoYXMgc2hhcmVkIHdp
dGggeW91IGEgSW1hZ2luZ1N0dWR5IHJlY29yZC4gWW91IGNhbiByZXZpZXcgaXQgYnkgY2xpY2tp
bmcgb24gdGhpcyBsaW5rDQo8YSBocmVmPSJodHRwczovL2VmZmVyZW50ZGV2LmF6dXJld2Vic2l0
cmU8L2E+DQo8L2JvZHk+DQo8L2h0bWw+DQo=
--_000_98UUW7B0H8U4EB3P8CQCSUEN2localhostlocaldomain_--

Solution

  • I have a few thoughts but no definitive answers for you because I don't have inside knowledge of how Google does their spam categorizing.

    1. Avoid using names like "User" and "Notifier" in email addresses. That screams "automated message" to me and Google likely has semantics that weigh that type of naming convention as having a "spam" and/or "fishing" bias.

    2. Try setting the Content-Transfer-Encoding of the text/plain and text/html parts to something other than base64; I might suggest quoted-printable:

      foreach (var part in message.BodyParts.OfType<TextPart> ())
          part.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
      
    3. This likely isn't an issue, but it's possible, so I'll mention it. Avoid using "@localhost.localdomain" in the Message-Id header. If MimeKit is generating those, it's because it doesn't know what your hostname is and couldn't figure it out, but you can override this behavior by doing this:

      message.MessageId = MimeUtils.GenerateMessageId ("efferenthealthllc.onmicrosoft.com");
      
    4. Maybe try removing the Content-Id header from the text/html part? Again, I don't see this is likely to be an issue, but it's worth a shot of none of the above changes fix things.

      foreach (var part in message.BodyParts.OfType<TextPart> ())
          part.ContentId = null;