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
Views
Replies
Total Likes
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
Yes tried with this "from" address field then only that value picked. I am not able to change at runtime
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
I have rechecked the above solution but not working.
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.
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
Email email;
email.setFrom("test@example.com") - Not working
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(); } } }
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.
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.
Views
Replies
Total Likes
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
Hi @sanketd27011989 ,
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)
Hi,
Email email;
email.setFrom("test@example.com")
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies