use of com.axelor.mail.SmtpAccount in project axelor-open-suite by axelor.
the class MailAccountServiceImpl method getMailAccount.
@Override
public com.axelor.mail.MailAccount getMailAccount(EmailAccount mailAccount) {
Integer serverType = mailAccount.getServerTypeSelect();
String port = mailAccount.getPort() <= 0 ? null : mailAccount.getPort().toString();
com.axelor.mail.MailAccount account;
if (serverType == EmailAccountRepository.SERVER_TYPE_SMTP) {
account = new SmtpAccount(mailAccount.getHost(), port, mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
} else if (serverType == EmailAccountRepository.SERVER_TYPE_IMAP) {
account = new ImapAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
} else {
account = new Pop3Account(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
}
Properties props = account.getSession().getProperties();
if (mailAccount.getFromAddress() != null && !"".equals(mailAccount.getFromAddress())) {
props.setProperty("mail.smtp.from", mailAccount.getFromAddress());
}
if (mailAccount.getFromName() != null && !"".equals(mailAccount.getFromName())) {
props.setProperty("mail.smtp.from.personal", mailAccount.getFromName());
}
account.setConnectionTimeout(CHECK_CONF_TIMEOUT);
return account;
}
use of com.axelor.mail.SmtpAccount in project axelor-open-suite by axelor.
the class MessageServiceImpl method sendByEmail.
@Override
@Transactional(rollbackOn = { Exception.class })
public Message sendByEmail(Message message, Boolean isTemporaryEmail) throws MessagingException, AxelorException {
EmailAccount mailAccount = message.getMailAccount();
if (mailAccount == null) {
return message;
}
log.debug("Sending email...");
MailAccountService mailAccountService = Beans.get(MailAccountService.class);
com.axelor.mail.MailAccount account = new SmtpAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), mailAccountService.getDecryptPassword(mailAccount.getPassword()), mailAccountService.getSecurity(mailAccount));
List<String> replytoRecipients = this.getEmailAddresses(message.getReplyToEmailAddressSet());
List<String> toRecipients = this.getEmailAddresses(message.getToEmailAddressSet());
List<String> ccRecipients = this.getEmailAddresses(message.getCcEmailAddressSet());
List<String> bccRecipients = this.getEmailAddresses(message.getBccEmailAddressSet());
if (toRecipients.isEmpty() && ccRecipients.isEmpty() && bccRecipients.isEmpty()) {
throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MESSAGE_6));
}
MailSender sender = new MailSender(account);
MailBuilder mailBuilder = sender.compose();
mailBuilder.subject(message.getSubject());
if (!Strings.isNullOrEmpty(mailAccount.getFromAddress())) {
String fromAddress = mailAccount.getFromAddress();
if (!Strings.isNullOrEmpty(mailAccount.getFromName())) {
fromAddress = String.format("%s <%s>", mailAccount.getFromName(), mailAccount.getFromAddress());
} else if (message.getFromEmailAddress() != null) {
if (!Strings.isNullOrEmpty(message.getFromEmailAddress().getAddress())) {
log.debug("Override from ::: {}", this.getFullEmailAddress(message.getFromEmailAddress()));
mailBuilder.from(this.getFullEmailAddress(message.getFromEmailAddress()));
} else {
throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, IExceptionMessage.MESSAGE_5);
}
}
mailBuilder.from(fromAddress);
}
if (replytoRecipients != null && !replytoRecipients.isEmpty()) {
mailBuilder.replyTo(Joiner.on(",").join(replytoRecipients));
}
if (!toRecipients.isEmpty()) {
mailBuilder.to(Joiner.on(",").join(toRecipients));
}
if (ccRecipients != null && !ccRecipients.isEmpty()) {
mailBuilder.cc(Joiner.on(",").join(ccRecipients));
}
if (bccRecipients != null && !bccRecipients.isEmpty()) {
mailBuilder.bcc(Joiner.on(",").join(bccRecipients));
}
if (!Strings.isNullOrEmpty(message.getContent())) {
mailBuilder.html(message.getContent());
}
if (!isTemporaryEmail) {
for (MetaAttachment metaAttachment : getMetaAttachments(message)) {
MetaFile metaFile = metaAttachment.getMetaFile();
mailBuilder.attach(metaFile.getFileName(), MetaFiles.getPath(metaFile).toString());
}
getEntityManager().flush();
getEntityManager().lock(message, LockModeType.PESSIMISTIC_WRITE);
// send email using a separate process to avoid thread blocking
sendMailQueueService.submitMailJob(mailBuilder, message);
} else {
// No separate thread or JPA persistence lock required
try {
mailBuilder.send();
} catch (IOException e) {
log.debug("Exception when sending email", e);
TraceBackService.trace(e);
}
log.debug("Email sent.");
}
return message;
}
Aggregations