Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Send email notification in AEM using custom configs

Avatar

Level 4

Hello Everyone,

 

For one of the custom component in our project, we are using OOTB Day CQ Mail service config to send emails. We need another such config or way to send another mail notification and this will be triggered from other custom component. Is there a way to implement such custom configs in AEM/java that can be used to send mail notification?

 

Thanks,

Supriya Hande

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @supriya-hande 
I haven't tested the approach below, but you can give it a try and see if it works. If successful, you can refine it for production by adding proper logging and error handling.

 

Config

 

@ObjectClassDefinition(name = "Custom Mail Service Configuration")
public @interface CustomMailServiceConfig {

    @AttributeDefinition(name = "SMTP Server")
    String smtpServer() default "smtp.example.com";

    @AttributeDefinition(name = "SMTP Port")
    int smtpPort() default 25;

    @AttributeDefinition(name = "SMTP Username")
    String smtpUser();

    @AttributeDefinition(name = "SMTP Password")
    String smtpPassword();

    @AttributeDefinition(name = "From Email")
    String fromEmail() default "noreply@example.com";
}

 

 

Service + impl

 

@Designate(ocd = CustomMailServiceConfig.class)
@Component(service = CustomMailService.class, immediate = true)
public class CustomMailService {

    @Reference
    private MailService mailService;

    private String smtpServer;
    private int smtpPort;
    private String smtpUser;
    private String smtpPassword;
    private String fromEmail;

    @Activate
    @Modified
    protected void activate(CustomMailServiceConfig config) {
        this.smtpServer = config.smtpServer();
        this.smtpPort = config.smtpPort();
        this.smtpUser = config.smtpUser();
        this.smtpPassword = config.smtpPassword();
        this.fromEmail = config.fromEmail();
    }

    public void sendEmail(String to, String subject, String message) {
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName(smtpServer);
            email.setSmtpPort(smtpPort);
            email.setAuthentication(smtpUser, smtpPassword);
            email.setFrom(fromEmail);
            email.setSubject(subject);
            email.setMsg(message);
            email.addTo(to);
            mailService.send(email);
        } catch (EmailException e) {
            // Handle exception
        }
    }
}

 

 



Arun Patidar

View solution in original post

3 Replies

Avatar

Community Advisor

If you already have some service which is sending emails. Any particular reason that you are looking for other options ? 
Essentially you should be able to use the same service in other component as well. 
ACS commons also provide similar API https://adobe-consulting-services.github.io/acs-aem-commons/features/e-mail/email-api/index.html which you can take a look at but interally it also uses the same Day CQ mail service config.

Avatar

Correct answer by
Community Advisor

Hi @supriya-hande 
I haven't tested the approach below, but you can give it a try and see if it works. If successful, you can refine it for production by adding proper logging and error handling.

 

Config

 

@ObjectClassDefinition(name = "Custom Mail Service Configuration")
public @interface CustomMailServiceConfig {

    @AttributeDefinition(name = "SMTP Server")
    String smtpServer() default "smtp.example.com";

    @AttributeDefinition(name = "SMTP Port")
    int smtpPort() default 25;

    @AttributeDefinition(name = "SMTP Username")
    String smtpUser();

    @AttributeDefinition(name = "SMTP Password")
    String smtpPassword();

    @AttributeDefinition(name = "From Email")
    String fromEmail() default "noreply@example.com";
}

 

 

Service + impl

 

@Designate(ocd = CustomMailServiceConfig.class)
@Component(service = CustomMailService.class, immediate = true)
public class CustomMailService {

    @Reference
    private MailService mailService;

    private String smtpServer;
    private int smtpPort;
    private String smtpUser;
    private String smtpPassword;
    private String fromEmail;

    @Activate
    @Modified
    protected void activate(CustomMailServiceConfig config) {
        this.smtpServer = config.smtpServer();
        this.smtpPort = config.smtpPort();
        this.smtpUser = config.smtpUser();
        this.smtpPassword = config.smtpPassword();
        this.fromEmail = config.fromEmail();
    }

    public void sendEmail(String to, String subject, String message) {
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName(smtpServer);
            email.setSmtpPort(smtpPort);
            email.setAuthentication(smtpUser, smtpPassword);
            email.setFrom(fromEmail);
            email.setSubject(subject);
            email.setMsg(message);
            email.addTo(to);
            mailService.send(email);
        } catch (EmailException e) {
            // Handle exception
        }
    }
}

 

 



Arun Patidar