0

I am building a C# application which has to send certain (no-reply) e-mails within my company.(Warnings, Notifications, etc. Internal addresses only)

As I have spoken to our IT manager, it is possible without any authentication, even without an existing sender address (I can give anything I want), through a protected SMTP Host, only for internal use.

He even tested it with some printer application to show me that it works. This is also explained here (Option 2): https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365

In my code, I get a "Relay Not Permitted" exception :

public static void Email(string htmlString, string subject, string address)
        {
            MailMessage message = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            message.From = new MailAddress("sender@internal_domain.com");
            message.To.Add(new MailAddress(address));
            message.Subject = subject;
            message.IsBodyHtml = true; 
            message.Body = htmlString;
            smtp.Port = 25;
            smtp.Host = "***.protection.outlook.com";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                smtp.Send(message);
                Console.WriteLine("E-mail Sent!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Message: " + ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine("Exception Inner:   " + ex.InnerException);
            }
        }

I have changed the SMTP Host and the sender address for security purposes. The host is otherwise reachable. The receiver's address is my own work e-mail address, given as parameter.

What should I look into?

Thanks in advance!

Eck_FG
  • 1
  • 3
  • may be port 25 is not supported. try to use 587 and use this System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; – Bala Dec 21 '22 at 04:13
  • Well, as I mentioned the IT manager assured me to use port 25. Anyways, he looked into it, and told me that for this use case, we should have MIcrosoft to configure a relay server, or either we have to set up a local Exchange server. Eventually, we decided to make a local exchange server, so that will be the solution. – Eck_FG Dec 22 '22 at 07:40

0 Answers0