Search code examples
c#emailmailkitsystem.net.mailmimekit

c# MimeKit mail not sent without error. System.Net.Mail works fine


I made a console application to test MimeKit, it's executed without errors but emails never arrives.

i usae same data with System.Net.Mail and all works fine.

What can be the problem?

MimeKit code:

            var email = new MimeMessage();
            email.Sender = MailboxAddress.Parse("[email protected]");
            email.To.Add(MailboxAddress.Parse("[email protected]"));
            email.To.Add(MailboxAddress.Parse("[email protected]"));
            email.Subject = "test";
            var builder = new BodyBuilder();

            builder.HtmlBody = "corpo";
            email.Body = builder.ToMessageBody();


            using var smtp = new SmtpClient();
            smtp.Connect("smtp.xxx.com", 25, SecureSocketOptions.None);
            smtp.Authenticate("ut", "pwd");
            smtp.SendAsync(email);
            smtp.Disconnect(true);

            Console.WriteLine("Email Sent.");

System.mail code:

using (MailMessage mm = new MailMessage())
            {
                mm.To.Add(new MailAddress("[email protected]"));
                mm.To.Add(new MailAddress("[email protected]"));
                mm.From = new MailAddress("[email protected]", "You");
                mm.ReplyToList.Add("[email protected]");
                mm.Subject = "vecchia mail";
                mm.Body = "con system.mail";
                mm.IsBodyHtml = false;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.xxx.com";
                smtp.EnableSsl = false;
                smtp.UseDefaultCredentials = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                NetworkCredential NetworkCred = new NetworkCredential("ut", "pwd");
                smtp.Credentials = NetworkCred;
                smtp.Port = 25;
                Console.WriteLine("Sending Email......");
                smtp.Send(mm);
                Console.WriteLine("Email Sent.");
            }

Solution

  • Thanks to @Klaus Gütter i got the error and then find the solution: a security rule that prevent connection between development machine and mail server, once deployed the application in test environment all works fine.

    thanks for suggestions