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!