0

I am having an issue with sending emails specifically to Gmail-related accounts, and I'll be darned if I know what the issue is. This is a C# ASP.NET project, by the way.

First, the following code works, as long as I am sending to any email account OTHER than a Gmail account:

var mail = new MailMessage {
    Subject = "test email",
    Body = "this is only a test",
    Priority = MailPriority.High,
    IsBodyHtml = true,
    From = new MailAddress ( "<outbound email here>" )
};
var msgID = Guid.NewGuid().ToString();
var sentBy="<outbound mail domain>";
mail.Headers.Add ( "message-id", $"<{msgID}>");
mail.Headers.Add ( "msg-id", $"<{msgID}@{sentBy}>");
mail.To.Add ( new MailAddress ( "<recipient email>" ) );
var smtpClient = new SmtpClient("<email server address>") {
    Port = 587,
    Credentials = new NetworkCredential("<sender's email address>", "<password>"),
};
smtpClient.Send ( mail );
            

I have removed email addresses and network credentials, obviously.

The code works, because as long as I send email to a NON-Gmail account, it comes through just fine. But anything going to a Gmail-related account never arrives.

I added the two lines in the code above to add a message ID to the header based on what I read in several older posts here about some mail servers, like Gmail, rejecting email messages that didn't include them, but it has not fixed the issue, and I'm out of ideas. My ISP says the SPF record for the mail server is fine, so according to them that's not the issue. Has anyone else encountered this recently, and if so, how did you fix it?

To clarify, the comments/answers I have received so far are appreciated, but as I stated in the OP, this is a problem with sending emails TO Gmail accounts. I am using my ISP's mail server to send them, and I am adding a message ID to the header to address what the log says, that the message is missing a message ID and won't be accepted. I can send emails to other non Gmail accounts just fine, and when I inspect the headers they show a message id. So I don't know why this continues to be an issue.

wovano
  • 4,543
  • 5
  • 22
  • 49
TC_Guy
  • 127
  • 9
  • 1
    Does this answer your question? [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Hille Nov 11 '22 at 13:38
  • "...then you have to turn on [*Less secure app access*](https://myaccount.google.com/lesssecureapps)..." ~https://stackoverflow.com/a/32336/7571526 – Hille Nov 11 '22 at 13:40
  • These emails are not being sent USING Gmail accounts. I am trying to send emails TO Gmail accounts using my ISP's mail server. I continue to get a message in the server log that the email is missing a message ID, even though I am assigning one as part of the code you see above. I can send emails to ANY other email accounts, as long as they aren't Gmail or Gmail-related accounts. I have sent emails successfully to my other non-Gmail accounts and checked the headers, and they show a message id. So I am very confused with this problem. – TC_Guy Nov 11 '22 at 23:34
  • If you are not using gmail then you should not have tagged this question Gmail. please remove the Gmail tag. – Linda Lawton - DaImTo Nov 12 '22 at 10:14
  • Gmail tag is COMPLETELY appropriate, because the issue involves not being able to send emails specifically to Gmail accounts. This is a Gmail-related issue. – TC_Guy Nov 12 '22 at 11:20
  • Do you have a proper SPF and DKIM records for your domain? That resolved the problem for other random people on the internet. https://support.google.com/mail/thread/173900861/message-could-not-be-delivered-due-to-missing-valid-messageid-header?hl=en – Hille Nov 14 '22 at 13:44
  • I have my ISP looking at that right now, although they're deathly slow in getting back to me! – TC_Guy Nov 14 '22 at 21:06

1 Answers1

0

So the answer to this is that the issue was related to changes Google made to policies for sending emails to Gmail accounts, which might require adjustments to the SPF record on the sending SMTP server. That was the case in this situation - the hosting company was slow to respond (took them more than a week) and had to elevate the issue to their Tier 3 techs, but once the SPF record was fixed to account for Google's changes, all was resolved.

TC_Guy
  • 127
  • 9