Add dynamic sending email
This commit is contained in:
@@ -0,0 +1,75 @@
|
|||||||
|
package com.axelor.apps.purchase.web;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.mail.Message;
|
||||||
|
import javax.mail.Session;
|
||||||
|
import javax.mail.Transport;
|
||||||
|
import javax.mail.internet.InternetAddress;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
|
||||||
|
import com.axelor.apps.message.service.MailAccountService;
|
||||||
|
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.mail.Authenticator;
|
||||||
|
import javax.mail.PasswordAuthentication;
|
||||||
|
import javax.mail.Session;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class EmailUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method to send simple HTML email
|
||||||
|
* @param session
|
||||||
|
* @param toEmail
|
||||||
|
* @param subject
|
||||||
|
* @param body
|
||||||
|
*/
|
||||||
|
public static void sendEmail(MailAccountService mailAccountService, String toEmail, String subject, String body){
|
||||||
|
System.out.println("TLSEmail Start");
|
||||||
|
Properties props = new Properties();
|
||||||
|
|
||||||
|
|
||||||
|
final String fromEmail = mailAccountService.getDefaultSender().getLogin().toString();
|
||||||
|
final String password = mailAccountService.getDefaultSender().getPassword().toString();
|
||||||
|
props.put("mail.smtp.host", mailAccountService.getDefaultSender().getHost());
|
||||||
|
props.put("mail.smtp.port", mailAccountService.getDefaultSender().getPort());
|
||||||
|
props.put("mail.smtp.auth", "true");
|
||||||
|
props.put("mail.smtp.starttls.enable", "true");
|
||||||
|
|
||||||
|
Authenticator auth = new Authenticator() {
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(fromEmail, password);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Session session = Session.getInstance(props, auth);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MimeMessage msg = new MimeMessage(session);
|
||||||
|
//set message headers
|
||||||
|
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
|
||||||
|
msg.addHeader("format", "flowed");
|
||||||
|
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
||||||
|
|
||||||
|
msg.setFrom(new InternetAddress(fromEmail, "ERP SOPHAL"));
|
||||||
|
|
||||||
|
msg.setReplyTo(InternetAddress.parse(fromEmail, false));
|
||||||
|
|
||||||
|
msg.setSubject(subject, "UTF-8");
|
||||||
|
|
||||||
|
msg.setText(body, "UTF-8");
|
||||||
|
|
||||||
|
msg.setSentDate(new Date());
|
||||||
|
|
||||||
|
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
|
||||||
|
System.out.println("Message is ready");
|
||||||
|
Transport.send(msg);
|
||||||
|
|
||||||
|
System.out.println("EMail Sent Successfully!!");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.axelor.apps.purchase.web;
|
package com.axelor.apps.purchase.web;
|
||||||
|
|
||||||
|
import com.axelor.apps.message.service.MailAccountService;
|
||||||
import com.axelor.apps.purchase.db.PurchaseOrder;
|
import com.axelor.apps.purchase.db.PurchaseOrder;
|
||||||
import com.axelor.apps.purchase.db.PurchaseRequest;
|
import com.axelor.apps.purchase.db.PurchaseRequest;
|
||||||
import com.axelor.apps.purchase.db.repo.PurchaseRequestRepository;
|
import com.axelor.apps.purchase.db.repo.PurchaseRequestRepository;
|
||||||
@@ -31,10 +32,12 @@ import com.axelor.exception.db.repo.TraceBackRepository;
|
|||||||
import com.axelor.i18n.I18n;
|
import com.axelor.i18n.I18n;
|
||||||
import com.axelor.inject.Beans;
|
import com.axelor.inject.Beans;
|
||||||
import com.axelor.mail.db.repo.MailFollowerRepository;
|
import com.axelor.mail.db.repo.MailFollowerRepository;
|
||||||
|
import com.axelor.meta.CallMethod;
|
||||||
import com.axelor.meta.schema.actions.ActionView;
|
import com.axelor.meta.schema.actions.ActionView;
|
||||||
import com.axelor.meta.schema.actions.ActionView.ActionViewBuilder;
|
import com.axelor.meta.schema.actions.ActionView.ActionViewBuilder;
|
||||||
import com.axelor.rpc.ActionRequest;
|
import com.axelor.rpc.ActionRequest;
|
||||||
import com.axelor.rpc.ActionResponse;
|
import com.axelor.rpc.ActionResponse;
|
||||||
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import wslite.json.JSONException;
|
import wslite.json.JSONException;
|
||||||
@@ -64,6 +67,7 @@ public class PurchaseRequestController {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
||||||
|
|
||||||
|
@Inject private MailAccountService mailAccountService;
|
||||||
|
|
||||||
public void confirmCart(ActionRequest request, ActionResponse response) {
|
public void confirmCart(ActionRequest request, ActionResponse response) {
|
||||||
Beans.get(PurchaseRequestService.class).confirmCart();
|
Beans.get(PurchaseRequestService.class).confirmCart();
|
||||||
@@ -233,4 +237,9 @@ public class PurchaseRequestController {
|
|||||||
Beans.get(MailFollowerRepository.class).follow(purchaseRequest, sup);
|
Beans.get(MailFollowerRepository.class).follow(purchaseRequest, sup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CallMethod
|
||||||
|
public void sendEmail(String email,String subject,String body){
|
||||||
|
EmailUtil.sendEmail(mailAccountService, email,subject, body);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user