use of org.springframework.mail.javamail.JavaMailSenderImpl in project perun by CESNET.
the class Utils method sendPasswordResetConfirmationEmail.
/**
* Sends email to user confirming his password was changed.
*
* @param user user to send notification for
* @param email user's email to send notification to
* @param namespace namespace the password was re-set
* @throws InternalErrorException
*/
public static void sendPasswordResetConfirmationEmail(User user, String email, String namespace) throws InternalErrorException {
// create mail sender
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("localhost");
// create message
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(email);
message.setFrom(BeansUtils.getCoreConfig().getMailchangeBackupFrom());
String instanceName = BeansUtils.getCoreConfig().getInstanceName();
message.setSubject("[" + instanceName + "] Password reset in namespace: " + namespace);
// get validation link params
String i = cipherInput(String.valueOf(user.getId()), false);
String m = cipherInput(namespace, false);
// Build message
String text = "Dear " + user.getDisplayName() + ",\n\nyour password in namespace \"" + namespace + "\" was successfully reset." + "\n\nThis message is automatically sent to all your email addresses registered in " + instanceName + " in order to prevent malicious password reset without your knowledge.\n\n" + "If you didn't request / perform password reset, please notify your administrators and support at " + BeansUtils.getCoreConfig().getMailchangeBackupFrom() + " to resolve this security issue.\n\n" + "Message is automatically generated." + "\n----------------------------------------------------------------" + "\nPerun - Identity & Access Management System";
message.setText(text);
mailSender.send(message);
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project perun by CESNET.
the class Utils method sendValidationEmail.
/**
* Send validation email related to requested change of users preferred email.
*
* @param user user to change preferred email for
* @param url base URL of running perun instance passed from RPC
* @param email new email address to send notification to
* @param changeId ID of change request in DB
* @throws InternalErrorException
*/
public static void sendValidationEmail(User user, String url, String email, int changeId) throws InternalErrorException {
// create mail sender
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("localhost");
// create message
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(email);
message.setFrom(BeansUtils.getCoreConfig().getMailchangeBackupFrom());
String instanceName = BeansUtils.getCoreConfig().getInstanceName();
message.setSubject("[" + instanceName + "] New email address verification");
// get validation link params
String i = Integer.toString(changeId, Character.MAX_RADIX);
String m = Utils.getMessageAuthenticationCode(i);
try {
// !! There is a hard-requirement for Perun instance
// to host GUI on same server as RPC like: "serverUrl/gui/"
URL urlObject = new URL(url);
// use default if unknown rpc path
String path = "/gui/";
if (urlObject.getPath().contains("/krb/")) {
path = "/krb/gui/";
} else if (urlObject.getPath().contains("/fed/")) {
path = "/fed/gui/";
} else if (urlObject.getPath().contains("/cert/")) {
path = "/cert/gui/";
}
StringBuilder link = new StringBuilder();
link.append(urlObject.getProtocol());
link.append("://");
link.append(urlObject.getHost());
link.append(path);
link.append("?i=");
link.append(URLEncoder.encode(i, "UTF-8"));
link.append("&m=");
link.append(URLEncoder.encode(m, "UTF-8"));
link.append("&u=" + user.getId());
// Build message
String text = "Dear " + user.getDisplayName() + ",\n\nWe've received request to change your preferred email address to: " + email + "." + "\n\nTo confirm this change please use link below:\n\n" + link + "\n\n" + "Message is automatically generated." + "\n----------------------------------------------------------------" + "\nPerun - Identity & Access Management System";
message.setText(text);
mailSender.send(message);
} catch (UnsupportedEncodingException ex) {
throw new InternalErrorException("Unable to encode validation URL for mail change.", ex);
} catch (MalformedURLException ex) {
throw new InternalErrorException("Not valid URL of running Perun instance.", ex);
}
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project OpenClinica by OpenClinica.
the class QueryServiceImpl method sendEmail.
private void sendEmail(String to, String subject, String body, Boolean htmlEmail) throws Exception {
try {
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) appContext.getBean("mailSender");
Properties javaMailProperties = mailSender.getJavaMailProperties();
if (null != javaMailProperties) {
if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
javaMailProperties.put("mail.smtp.localhost", "localhost");
}
}
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
helper.setFrom(EmailEngine.getAdminEmail());
helper.setTo(processMultipleImailAddresses(to.trim()));
helper.setSubject(subject);
helper.setText(body, true);
mailSender.send(mimeMessage);
logger.debug("Email sent successfully on {}", new Date());
} catch (MailException me) {
logger.error("Email could not be sent on {} due to: {}", new Date(), me.getMessage());
}
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project coprhd-controller by CoprHD.
the class MailHelper method createJavaMailSender.
private JavaMailSenderImpl createJavaMailSender() {
Map<String, String> properties = getPropertiesFromCoordinator();
smtpServer = properties.get(SMTP_SERVER);
smtpPort = properties.get(SMTP_PORT);
smtpEnableTls = properties.get(SMTP_ENABLE_TLS);
fromAddress = properties.get(SMTP_FROM_ADDRESS);
smtpAuthType = properties.get(SMTP_AUTH_TYPE);
username = properties.get(SMTP_USERNAME);
password = properties.get(SMTP_PASSWORD);
boolean enableTls = StringUtils.equalsIgnoreCase("yes", smtpEnableTls);
boolean authEnabled = StringUtils.isNotBlank(smtpAuthType) && !StringUtils.equalsIgnoreCase(smtpAuthType, "none");
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.smtp.host", smtpServer);
javaMailProperties.setProperty("mail.smtp.connectiontimeout", SMTP_CONNECTION_TIMEOUT);
javaMailProperties.setProperty("mail.smtp.timeout", SMTP_READ_TIMEOUT);
javaMailProperties.setProperty("mail.smtp.writetimeout", SMTP_WRITE_TIMEOUT);
if (enableTls) {
javaMailProperties.setProperty("mail.smtp.channel", "starttls");
smtpPort = defaultPort(smtpPort, DEFAULT_SMTP_TLS_PORT);
} else {
smtpPort = defaultPort(smtpPort, DEFAULT_SMTP_PORT);
}
javaMailProperties.setProperty("mail.smtp.port", smtpPort);
javaMailProperties.setProperty("mail.smtp.from", fromAddress);
if (authEnabled) {
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.smtp.user", username);
javaMailProperties.setProperty("mail.smtp.pass", password);
}
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(smtpServer);
try {
mailSender.setPort(Integer.parseInt(smtpPort));
} catch (NumberFormatException e) {
log.error(String.format("Failed to parse smtp port [%s]", smtpPort), e);
}
// this.mailSender.setProtocol(protocol);
if (authEnabled) {
mailSender.setUsername(username);
mailSender.setPassword(password);
}
mailSender.setJavaMailProperties(javaMailProperties);
log.debug(String.format(" mail.smtp.host = %s", mailSender.getJavaMailProperties().get("mail.smtp.host")));
log.debug(String.format(" mail.smtp.port = %s", mailSender.getJavaMailProperties().get("mail.smtp.port")));
log.debug(String.format(" mail.smtp.channel = %s", mailSender.getJavaMailProperties().get("mail.smtp.channel")));
log.debug(String.format(" mail.smtp.auth = %s", mailSender.getJavaMailProperties().get("mail.smtp.auth")));
log.debug(String.format(" mail.smtp.user = %s", mailSender.getJavaMailProperties().get("mail.smtp.user")));
log.debug(String.format(" mail.smtp.from = %s", mailSender.getJavaMailProperties().get("mail.smtp.from")));
return mailSender;
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project ocvn by devgateway.
the class EmailServiceConfiguration method javaMailSenderImpl.
@Bean
public JavaMailSenderImpl javaMailSenderImpl() {
JavaMailSenderImpl jmsi = new JavaMailSenderImpl();
jmsi.setHost("localhost");
jmsi.setPort(SMTP_PORT);
return jmsi;
}
Aggregations