Mailkit: Cant send MimeMessage to Smtp server

Created on 22 Feb 2019  路  4Comments  路  Source: jstedfast/MailKit

Hello,
i'm trying to send a Mimemessage to a SmtpServer, which is not in my Network. Now the point is as following:
if i do this with System.Net.Smtp it works fine, but when i try to send the message with Mailkit.Net.Smtp it throws this Exception:
Exceptionmessage: 5.7.1 <[email protected]>: Recipient address rejected: Access denied
When i try to authenticate through smtpclient.Authenticate() it returns, that the smpt doesn't support Authorization.
Now my question is: is your smtpclient.Send()-Method implemented different than the .Net one?
Here are the implementations (i changed the emailaddresses and the Smtpserver):
.Net:

class Program
    {
        static string host = "smtp.server";
        static int port = 25;
        static void Main(string[] args)
        {
            string to = "[email protected]";
            string from = "[email protected]";
            MailMessage message = new MailMessage(from, to);
            message.Subject = "Using the new SMTP client.";
            message.Body = @"some Messagebody";
            SmtpClient client = new SmtpClient(host, port);
            client.UseDefaultCredentials = true;
            Console.WriteLine("Setup Complete. Preparing send . . .");

            try
            {
                Console.WriteLine("Sending . . .");
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                            ex.ToString());
            }
        }
    }

Mailkit (i created a second class, that initalizes my Message)
Main:

class Program
    {
        static string host = "smtp.server";
        static int port = 25;
        static void Main(string[] args)
        {
            Dictionary<string, string> trace = new Dictionary<string, string>();
            Console.WriteLine("Setting Up Smtp Client and Message . . .");
            var client = new SmtpClient();
            var message = new Message();
            client.Connect(host, port, false);
            trace.Add("Setup", "Connection established");

            Console.WriteLine("Gathering Content for Message . . .");
            message.InitalizeMessage();
            trace.Add("Gathered Mailcontent", "finished");

            try
            {
                client.Send(message.MailMessage);
                trace.Add("ASyncSend", "Email Sent");

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exceptionmessage: \n{0}", ex.Message);
            }

            Console.WriteLine(trace["Setup"]);
            Console.WriteLine(trace["Authorization"]);
            Console.WriteLine(trace["Gathered Mailcontent"]);
            Console.WriteLine(trace["ASyncSend"]);
            Console.ReadKey();
        }
    }

And here the Message class:

class Message
    {
        /// <summary>
        /// MimeMessage in which the mailcontent is delivered
        /// </summary>
        public MimeMessage MailMessage { get; private set; }

        /// <summary>
        /// The sender of the email
        /// </summary>
        public MailboxAddress Sender { get; private set; }

        /// <summary>
        /// Recipient of the email
        /// </summary>
        public MailboxAddress Recipient { get; private set; }

        /// <summary>
        /// Subject of the Mail
        /// </summary>
        public string Subject { get; private set; }

        /// <summary>
        /// Constructor of Messageclass
        /// </summary>
        public Message()
        {
            MailMessage = new MimeMessage();
            Sender = new MailboxAddress("[email protected]");
            Recipient = new MailboxAddress("[email protected]");
            Subject = "Testmail Smtp";
        }

        /// <summary>
        /// Adds The Sender, Recipient and the subject
        /// and calls the InsertContent() Function to fill Message with content
        /// </summary>
        public void InitalizeMessage()
        {
            MailMessage.From.Add(Sender);
            MailMessage.To.Add(Recipient);
            MailMessage.Subject = Subject;
            InsertContent();
        }

        /// <summary>
        /// inserts some static testcontent in the emailmessage
        /// </summary>
        private void InsertContent()
        {
            BodyBuilder builder = new BodyBuilder();
            builder.TextBody = @"some Plainmessage";
            builder.HtmlBody = string.Format(@"<p>some Htmlmessage</p>");
            MailMessage.Body = builder.ToMessageBody();
        }
    }
question

All 4 comments

That's an error message from the server saying that it will not allow you to send mail to that recipient.

If you can get a protocol log that would be helpful.

https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog

When you get a protocol log, please update this bug report and I will take a closer look.

Thank you, for your fast response. Here is the log:

_Connected to smtp:\someserver
S: 220 some.server.org (Erl) ESMTP Postfix
C: EHLO [00.00.00.00(I changed this IP Address)]
S: 250-some.server.org
S: 250-PIPELINING
S: 250-SIZE 26214400
S: 250-VRFY
S: 250-ETRN
S: 250-ENHANCEDSTATUSCODES
S: 250 8BITMIME
C: MAIL FROM:your@email.com BODY=8BITMIME
C: RCPT TO:your@email.com
S: 250 2.1.0 Ok
S: 554 5.7.1 your@email.com: Recipient address rejected: Access denied
C: RSET
S: 250 2.0.0 Ok_

As i understand it has nothing to do with the connection to the server but with the Access. Am i right?

I solved it. As i thought, it was a problem with the access of my virtual machine. It was not enabled on the smtpserver.
Thank you for your great and fast help!

Was this page helpful?
0 / 5 - 0 ratings