Send Email via O365 in Code

I am currently sending emails from code in Epicor Functions using the GetMailer() method that returns an ISmtpMailer.

I am wanting to change our emails from SMTP to Microsoft 365. I have set up the M365 Email Settings successfully, but any functions using GetMailer/ISmtpMailer are still sending via SMTP.

Anyone know how I can send emails via M365 from code in Functions?

You can send VIA M365 using SMTP protocol. What exactly do you mean by this?

To configure your email client or application to send emails through Office 365 (Microsoft 365), you'll need the following SMTP settings: Server name: smtp.office365.com, Port: 587 (recommended) or 25, Encryption: STARTTLS or SSL/TLS, Username: your full Office 365 email address, and Password: your Office 365 password. 
Detailed Breakdown of Settings:
SMTP Server Name: smtp.office365.com. 
SMTP Port:
   587 (recommended): This port uses STARTTLS encryption, providing a secure connection for sending email. 
   25: This port can be used with TLS/SSL encryption, but port 587 is preferred for security. 
Encryption:
STARTTLS: This is the recommended method and encrypts the connection after it's established. 
SSL/TLS: Some clients may also support SSL/TLS encryption. 
Username: Your full Office 365 email address (e.g., yourname@yourdomain.com). 
Password: Your Office 365 account password. 
Authentication: Ensure SMTP authentication is enabled. 
Firewall and Network: Make sure your firewall and network settings permit outgoing connections on the specified port (587 or 25) to smtp.office365.com. 

You can also configure a SMTP drop address e.g.: mydomain.mail.protection.outlook.com
Link: How to set up a multifunction device or application to send email using Microsoft 365 or Office 365 | Microsoft Learn

In Email Settings Maintenance you can set up the system to use Microsoft 365 mail directly via App Registration, not using SMTP. I don’t know of a way, in code, to take advantage of this form of email delivery. Epicor is doing it somehow, I’d like to do it in my Functions as well.

1 Like

Something like that

var mailSender = Ice.Lib.EmailSender.EmailSender.Create("EPIC06", "manager") as Ice.Lib.EmailSender.IEmailSender;

mailSender.SendBy("MsGraph",...) // all other paramters like any email

add reference to Ice.Lib.EmailSender.dll

1 Like

Did you remove the smtp settings when you set up the other?

I’m not certain, but I think that class actually just uses the default mailer.

yes, it uses mailer selected for System/BPM in Email settings show above

2 Likes

Yup the GetMailer ISMTP Mailer works fine.

I set the defaults at the top to M365.

I just set up m365 mail in mine.
I changed the smtp server to a bs value so it wouldn’t work.

Ran an email in a function with getmailer.

I got it. It had to come from msgraph.

1 Like

Thank you everyone! My mistake.. I was under the impression that the Email Settings were applied system-wide, not per-company.

My function was running under a different company than the one I configured for email via Microsoft 365 Graph.

Once I configured the other companies, it is working as expected.

2 Likes