Send email notification in AEM using custom configs | Community
Skip to main content
supriya-hande
Level 4
August 22, 2024
Solved

Send email notification in AEM using custom configs

  • August 22, 2024
  • 3 replies
  • 889 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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 } } }

 

 

3 replies

MukeshYadav_
Community Advisor
Community Advisor
August 22, 2024
h_kataria
Community Advisor
Community Advisor
August 22, 2024

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.

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 22, 2024

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