Fix email sending using smtp (FINERACT-1070)
This commit is contained in:
committed by
Yemdjih Kaze Nasser
parent
2725d239dc
commit
153204bad7
+3
-3
@@ -72,7 +72,7 @@ public class ExternalServicesPropertiesReadPlatformServiceImpl implements Extern
|
||||
String username = null;
|
||||
String password = null;
|
||||
String host = null;
|
||||
String port = "25";
|
||||
String port = "587";
|
||||
boolean useTLS = false;
|
||||
String fromEmail = null;
|
||||
String fromName = null;
|
||||
@@ -83,12 +83,12 @@ public class ExternalServicesPropertiesReadPlatformServiceImpl implements Extern
|
||||
|
||||
if (ExternalServicesConstants.SMTP_USERNAME.equalsIgnoreCase(name)) {
|
||||
username = value;
|
||||
} else if (ExternalServicesConstants.SMTP_PORT.equalsIgnoreCase(port)) {
|
||||
port = value;
|
||||
} else if (ExternalServicesConstants.SMTP_PASSWORD.equalsIgnoreCase(name)) {
|
||||
password = value;
|
||||
} else if (ExternalServicesConstants.SMTP_HOST.equalsIgnoreCase(name)) {
|
||||
host = value;
|
||||
} else if (ExternalServicesConstants.SMTP_PORT.equalsIgnoreCase(name)) {
|
||||
port = value;
|
||||
} else if (ExternalServicesConstants.SMTP_USE_TLS.equalsIgnoreCase(name)) {
|
||||
useTLS = Boolean.parseBoolean(value);
|
||||
} else if (ExternalServicesConstants.SMTP_FROM_EMAIL.equalsIgnoreCase(name)) {
|
||||
|
||||
+28
-18
@@ -18,14 +18,13 @@
|
||||
*/
|
||||
package org.apache.fineract.infrastructure.core.service;
|
||||
|
||||
import org.apache.commons.mail.DefaultAuthenticator;
|
||||
import org.apache.commons.mail.Email;
|
||||
import org.apache.commons.mail.EmailException;
|
||||
import org.apache.commons.mail.SimpleEmail;
|
||||
import java.util.Properties;
|
||||
import org.apache.fineract.infrastructure.configuration.data.SMTPCredentialsData;
|
||||
import org.apache.fineract.infrastructure.configuration.service.ExternalServicesPropertiesReadPlatformService;
|
||||
import org.apache.fineract.infrastructure.core.domain.EmailDetail;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@@ -55,31 +54,42 @@ public class GmailBackedPlatformEmailService implements PlatformEmailService {
|
||||
|
||||
@Override
|
||||
public void sendDefinedEmail(EmailDetail emailDetails) {
|
||||
final Email email = new SimpleEmail();
|
||||
final SMTPCredentialsData smtpCredentialsData = this.externalServicesReadPlatformService.getSMTPCredentials();
|
||||
|
||||
final String authuser = smtpCredentialsData.getUsername();
|
||||
final String authpwd = smtpCredentialsData.getPassword();
|
||||
|
||||
// Very Important, Don't use email.setAuthentication()
|
||||
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
|
||||
email.setDebug(false); // true if you want to debug
|
||||
email.setHostName(smtpCredentialsData.getHost());
|
||||
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost(smtpCredentialsData.getHost()); // smtp.gmail.com
|
||||
mailSender.setPort(Integer.parseInt(smtpCredentialsData.getPort())); // 587
|
||||
|
||||
// Important: Enable less secure app access for the gmail account used in the following authentication
|
||||
|
||||
mailSender.setUsername(authuser); // use valid gmail address
|
||||
mailSender.setPassword(authpwd); // use password of the above gmail account
|
||||
|
||||
Properties props = mailSender.getJavaMailProperties();
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.debug", "true");
|
||||
|
||||
try {
|
||||
if (smtpCredentialsData.isUseTLS()) {
|
||||
// FINERACT-1070: NOT email.setSSLOnConnect(true); email.setSslSmtpPort(smtpCredentialsData.getPort());
|
||||
email.setStartTLSRequired(true);
|
||||
// Needs to disable startTLS if the port is 465 in order to send the email successfully when using the
|
||||
// smtp.gmail.com as the host
|
||||
if (smtpCredentialsData.getPort().equals("465")) {
|
||||
props.put("mail.smtp.starttls.enable", "false");
|
||||
}
|
||||
}
|
||||
email.setSmtpPort(Integer.parseInt(smtpCredentialsData.getPort()));
|
||||
email.setFrom(smtpCredentialsData.getFromEmail(), smtpCredentialsData.getFromName());
|
||||
|
||||
email.setSubject(emailDetails.getSubject());
|
||||
email.setMsg(emailDetails.getBody());
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(smtpCredentialsData.getFromEmail()); // same email address used for the authentication
|
||||
message.setTo(emailDetails.getAddress());
|
||||
message.setSubject(emailDetails.getSubject());
|
||||
message.setText(emailDetails.getBody());
|
||||
mailSender.send(message);
|
||||
|
||||
email.addTo(emailDetails.getAddress(), emailDetails.getContactName());
|
||||
email.send();
|
||||
} catch (EmailException e) {
|
||||
} catch (Exception e) {
|
||||
throw new PlatformEmailSendException(e);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ insert into c_external_service_properties (`name`, `value`, `external_service_id
|
||||
|
||||
insert into c_external_service_properties (`name`, `value`, `external_service_id`) values('host', 'smtp.gmail.com', (select id from c_external_service where name = 'SMTP_Email_Account'));
|
||||
|
||||
insert into c_external_service_properties (`name`, `value`, `external_service_id`) values('port', '25', (select id from c_external_service where name = 'SMTP_Email_Account'));
|
||||
insert into c_external_service_properties (`name`, `value`, `external_service_id`) values('port', '587', (select id from c_external_service where name = 'SMTP_Email_Account'));
|
||||
|
||||
insert into c_external_service_properties (`name`, `value`, `external_service_id`) values('useTLS', 'true', (select id from c_external_service where name = 'SMTP_Email_Account'));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user