abpframework / abp

Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.68k stars 3.39k forks source link

M365 SMTP Relay (Exchange Online) and configuration in abp #18813

Open leonkosak opened 7 months ago

leonkosak commented 7 months ago

Has anyone tried to establish sending emails via Exchange Online (Microsoft 365 - Business plans, not Personal) via abp application (which is hosted on server with unique IP address)?

I know, that SMTP Relay for specific IP address should be configured as described here: https://lazyadmin.nl/office-365/smtp-relay-in-office-365/

But I haven't figured out how to properly configure abp emailing settings: https://docs.abp.io/en/abp/latest/Emailing#email-settings

Thank you for your suggestions.

leonkosak commented 7 months ago

I found a solution how to send email via M365 SMT Relay from scratch in C#, but I still haven't found how to properly set abp emailing configuration.

Here is a sample c# application:

using System;
using System.Net.Mail;

class Program
{
    static void Main()
    {
        string senderEmail = "sender@domain.com";
        string senderDisplayName = "Sender Name";
        MailAddress mailAddressSender = new MailAddress(senderEmail, senderDisplayName);

        string recipientEmail = "recipient@domain.com";
        MailAddress recipientAddress = new MailAddress(recipientEmail);

        MailMessage message = new MailMessage(mailAddressSender, recipientAddress);

        message.From = mailAddressSender;
        message.Subject = "Hello from .NET Console App";
        message.Body = "This is the email body.";

        string host = "<name>.mail.protection.outlook.com";
        int port = 25;

        SmtpClient smtpClient = new SmtpClient(host, port);
        smtpClient.EnableSsl = true;

        try
        {
            Console.WriteLine("Try Email send.");
            smtpClient.Send(message);
            Console.WriteLine("Email sent successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Failed to send email. Error message: " + ex.Message);
        }
    }
}

@maliming do you maybe have suggestions for this question? Thank you

maliming commented 7 months ago

hi

Add your EmailSender and override the BuildMailMessage and BuildClientAsync method

If you need additional parameters, please use: https://github.com/abpframework/abp/pull/17582