简体   繁体   中英

On Send email, MailKit throws an error in C#

I'm trying to send an email with the MailKit. However, it was working before and now suddenly is not working anymore. I'm able to connect & authenticate but when it reaches the send email, is throwing an error.

Error code: MessageNotAccepted

Error message: 6.6.0 Error sending message for delivery.

Here is my code

        var message = new MimeMessage();
        message.From.Add(new MailboxAddress(contactModel.Name, "ssssss@yahoo.com"));
        // This needs to be put in a configuration file
        message.To.Add(new MailboxAddress("NorbertDev", "sssssss@gmail.com"));
        message.Subject = $"{contactModel.Name} contacted me!";
        message.Body = new TextPart("plain") {
            Text = contactModel.Message + 
            " Details of sender: " + contactModel.EmailAddress + ", " + contactModel.ContactNumber + " ," + contactModel.Name
        };

        using (var client = new SmtpClient())
        {
            try
            {
                client.SslProtocols = System.Security.Authentication.SslProtocols.Tls11 |
System.Security.Authentication.SslProtocols.Tls12 |
System.Security.Authentication.SslProtocols.Tls |
System.Security.Authentication.SslProtocols.Ssl3 |
System.Security.Authentication.SslProtocols.Ssl2;
                if (!client.IsConnected)
                {
                    await client.ConnectAsync("smtp.mail.yahoo.com", 587, false).ConfigureAwait(false);
                }
                if (!client.IsAuthenticated)
                {
                    await client.AuthenticateAsync("ssssss@yahoo.com", "aaaaaaaaaaa").ConfigureAwait(false);
                }

                await client.SendAsync(message).ConfigureAwait(false); <-- Here is the error
                await client.DisconnectAsync(true).ConfigureAwait(false);

                return "success";
            }
            catch (Exception e)
            {
                throw new Exception($"The email from {contactModel.EmailAddress} captured but not sent to the owner");
            }

        }

Try getting a Protocol Log . It may help diagnose the problem.

Based on https://support.mozilla.org/en-US/questions/1268916 it seems that some users on Thunderbird have gotten the same error. It may be worth reading what sort of things caused the issue for those Thunderbird users to see if any of them apply to you as well.

For example, not having any addresses in the To header and/or adding yourself to the Bcc list seems to cause this sort of issue. I'm not clear if there are any other things that can cause this, but the fact that other mail clients are also experiencing this very odd error message from Yahoo Mail! suggests to me that this may be a Yahoo Mail! bug.

In any event, the problem you are hitting does not seem to be related to authentication, it seems to just be sort sort of aggressive spam filtering being done by Yahoo Mail's SMTP server in rejecting messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM