Can I update "From" address/ Sender from Day CQ mail service at runtime ? | Community
Skip to main content
Level 3
June 13, 2022

Can I update "From" address/ Sender from Day CQ mail service at runtime ?

  • June 13, 2022
  • 6 replies
  • 3410 views

Hi Leads,

Thanks in advance.

 

I want to change the sender means "From" address while using Day Mail Service in AEM 6.5. I have tried but it takes only STMP user email as sender. New values are not picked for sending mail- email.setFrom(from);

 

 

 Regards

Sanket

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

6 replies

ArpitVarshney
Community Advisor
Community Advisor
June 13, 2022

Hey @user1576 

In the screenshot, I don't see any email in the field "From address". Have you tried putting some email there and triggering your functionality? I think that email would pick in your "From" in the email.

 

Regards,

Arpit Varshney

user1576Author
Level 3
June 13, 2022

Yes tried with this "from" address field  then only that value picked. I am not able to change at runtime

ArpitVarshney
Community Advisor
Community Advisor
June 13, 2022

Hey @user1576 

Have you made sure to set this variable before calling send() method?
You can see the snippet here for your reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/messagegatewayservice/m-p/252196 

Regards,

Arpit Varshney

Pallavi_Shukla_
Community Advisor
Community Advisor
June 13, 2022

@user1576 

 You can change this value by configuring the Day CQ mail service

service in the Web Console.

To configure the from-email address, add a sling:OsgiConfig node to the repository. Use the following procedure to add the node directly using CRXDE Lite

When working with AEM there are several methods of managing the configuration settings for such services; see Configuring OSGi for more details and the recommended practices.

https://experienceleague.adobe.com/docs/experience-manager-65/administering/operations/notification.html?lang=en

JeevanRaj
Community Advisor
Community Advisor
June 13, 2022

Hi @user1576 

 

Yes, you can update the "From Address". If you are using the Email object then you can set the from address to it.

Email email;
email.setFrom("test@example.com")

I tested it out and was able to change the from address dynamically. Please let me know if this works for you.

 

Thanks

Jeevan

 

user1576Author
Level 3
June 14, 2022
Email email;
email.setFrom("test@example.com") - Not working

 

JeevanRaj
Community Advisor
Community Advisor
June 14, 2022

I'm not sure why it didn't work for you. I have written a sample servlet to test this scenario. Pasting the whole servlet code below.

 

import com.day.cq.mailer.MailService;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;


@Component(service = Servlet.class,
        property = {
                "sling.servlet.paths=" + "/bin/abc/testmail",
                "sling.servlet.methods=" + "GET"
        })
public class TestMailServlet extends SlingSafeMethodsServlet {

    @Reference
    protected transient MailService mailService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        try {
            Email email = new MultiPartEmail();
            email.setFrom("from@example.com");
            email.addTo("jeevan@example.com");
            mailService.send(email);
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

 

user1576Author
Level 3
June 15, 2022

Hi All,

 I have used it the below code and it's working as of now.but thing this is not proper way.

 

Configuration configuration = configAdmin.getConfiguration("com.day.cq.mailer.DefaultMailService");

if (configuration != null) {

// get properties

Dictionary<String, Object> properties = configuration.getProperties();

Dictionary<String, Object> newProperties = properties;

// update properties
if(properties == null)
{
newProperties = new Hashtable<String, Object>();
}

// update new properties

configuration.update(newProperties);

 

// revert to old property

configuration.update(properties);

 

 

Thanks Sanket

 

 

shabarish
Level 2
June 15, 2022

Hi @user1576 ,

I think Gmail wont allow you to change the from address

Please check this Stack overflow thread.

java - how to set correct from email address in javamail? - Stack Overflow

and Google Documentation

Send emails from a different address or alias - Gmail Help (google.com)

lavishvasuja
Level 3
August 22, 2023

Hi,

 

Set up an SMTP using your desired email address for sending emails (e.g., test@example.com). Then, configure/add this SMTP within the SMTP, which was configured in settings of the AEM Day Mail Service.
 
Email email;
email.setFrom("test@example.com")