Expand my Community achievements bar.

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

Avatar

Level 4

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);

 

sanketd27011989_0-1655119994042.png

 

 Regards

Sanket

14 Replies

Avatar

Community Advisor

Hey @sanketd27011989 

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

Avatar

Level 4

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

Avatar

Community Advisor

Hey @sanketd27011989 

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/... 

Regards,

Arpit Varshney

Avatar

Community Advisor

@sanketd27011989 

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

Avatar

Community Advisor

Hi @sanketd27011989 

 

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.

JeevanRaj_0-1655129614685.png

 

Thanks

Jeevan

 

Avatar

Level 4
Email email;
email.setFrom("test@example.com") - Not working

 

Avatar

Community Advisor

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();
        }
    }
}

JeevanRaj_0-1655220322248.png

 

Avatar

Level 4

 

I have tried your solution but still, I am getting mail from the sender which is configured in the AEM CQ mail service with valid credentials.

I am getting emails on my Gmail ID.but the new sender is not reflected.

Avatar

Community Advisor

Is there any specific reason you have for using Gmail as SMTP? If it is only for testing in your local you can use fakeSMTP which is great for testing email functionalities. The setup is pretty easy and straightforward. Do let me know if you have queries relating to setting it up.

 

If you have to use only Gmail for any specific reason, do let me know and I'll give it a try with Gmail as an SMTP server and let you know how it goes.

Avatar

Level 2

@sanketd27011989 

 

I am also facing the same issue are you able to manage it?

Avatar

Level 4

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

 

 

Avatar

Level 2

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")