-1

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("aa@sbb.com");
            email.To.Add(MailboxAddress.Parse("aa.cc@dd.com"));
            email.To.Add(MailboxAddress.Parse("bb.dd@dd.com"));
            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("aa.cc@dd.com"));
                mm.To.Add(new MailAddress("bb.dd@dd.com"));
                mm.From = new MailAddress("aa@sbb.com", "You");
                mm.ReplyToList.Add("aa@sbb.com");
                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.");
            }
gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
  • 2
    You have to `await` the `SendAsync` call. – Klaus Gütter Sep 15 '21 at 08:34
  • "But then I get an error about await can only be used in an async method".. "And when I make it async it gives another message 'because this call is not awaited..' in the calling code so I make that await and then I get back to the first error but in the caller..".. -> Yep. Keep going, async all the way up the tree please; that's how we get those threads to go off and do something useful while they wait for the IO to complete – Caius Jard Sep 15 '21 at 08:44
  • thanks, now got error: Sending email with MailKit: 5.7.60 SMTP; Client does not have permissions to send as this sender . i can search for solution – gt.guybrush Sep 15 '21 at 09:27
  • Many SMTP servers require that the `From` address "belongs" to that mail server. They also tend to require that the username used to authenticate matches the `From` address. – jstedfast Sep 15 '21 at 15:02
  • BTW, instead of making everything async, you could have also opted to use `smtp.Send(email);` – jstedfast Sep 15 '21 at 15:03

1 Answers1

0

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

gt.guybrush
  • 1,320
  • 3
  • 19
  • 48