Skip to content

Commit

Permalink
core: make MailUtils class more generic, #TASK-7192
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Jan 21, 2025
1 parent a2dc078 commit 727b48e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,10 @@ public OpenCGAResult resetPassword(String organizationId, String userId) throws
throw new CatalogException("Could not retrieve the user e-mail.");
}

String email = user.first().getEmail();

String mailUser = this.emailConfig.getUser();
String mailPassword = this.emailConfig.getPassword();
String mailHost = this.emailConfig.getHost();
String mailPort = this.emailConfig.getPort();
try {
MailUtils.sendResetPasswordMail(email, newPassword, mailUser, mailPassword, mailHost, mailPort, userId);
String email = user.first().getEmail();
String resetMailContent = MailUtils.getResetMailContent(userId, newPassword);
MailUtils.configure(this.emailConfig).sendMail(email, "XetaBase: Password Reset", resetMailContent);
result = dbAdaptorFactory.getCatalogUserDBAdaptor(organizationId).resetPassword(userId, email, newPassword);
} catch (Exception e) {
throw new CatalogException("Email could not be sent.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.opencb.opencga.core.common;

import org.opencb.opencga.core.config.Email;
import org.opencb.opencga.core.exceptions.MailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -25,19 +27,26 @@
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MailUtils {

private static final Logger logger = LoggerFactory.getLogger(MailUtils.class);

public static void sendResetPasswordMail(String to, String newPassword, final String mailUser, final String mailPassword,
String mailHost, String mailPort, String userId) throws Exception {
private final Email email;

private MailUtils(Email emailConfig) {
this.email = emailConfig;
}

public static MailUtils configure(Email emailConfig) {
return new MailUtils(emailConfig);
}

public void sendMail(String targetMail, String subject, String content) throws MailException {
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.port", mailPort);
props.put("mail.smtp.host", email.getHost());
props.put("mail.smtp.port", email.getPort());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
Expand All @@ -49,65 +58,39 @@ public static void sendResetPasswordMail(String to, String newPassword, final St
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailUser, mailPassword);
return new PasswordAuthentication(email.getUser(), email.getPassword());
}
});
logger.info("Sending reset password from" + mailUser + " to " + to + " using " + mailHost + ":" + mailPort);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailUser));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

message.setSubject("XetaBase: Password Reset");
message.setText(getEmailContent(userId,newPassword));
Transport.send(message);
logger.info("Sending email from '{}' to '{}' using '{}:{}'", email.getUser(), targetMail, email.getHost(), email.getPort());

try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email.getUser()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(targetMail));

message.setSubject(subject);
message.setText(content);
Transport.send(message);
} catch (Exception e) {
throw new MailException("Could not send email.", e);
}
}

public static String getEmailContent(String userId, String temporaryPassword) {
StringBuilder sb = new StringBuilder();

sb.append("Hi ").append(userId).append(",\n\n");
sb.append("We confirm that your password has been successfully reset.\n\n");
sb.append("Please find your new login credentials below:\n\n");
sb.append("User ID: ").append(userId).append("\n");
sb.append("Temporary Password: ").append(temporaryPassword).append("\n\n");
sb.append("For your security, we strongly recommend that you log in using the temporary password provided ");
sb.append("and promptly create a new password that is unique and known only to you. ");
sb.append("You can change your password by accessing \"Your Profile > Change Password\" in your User Profile.\n\n");
sb.append("If you did not request a password reset, please contact our support team immediately at [email protected].\n\n");
sb.append("Best regards,\n\n");
sb.append("ZettaGenomics Support Team \n\n");



return sb.toString();
public static String getResetMailContent(String userId, String temporaryPassword) {
return new StringBuilder()
.append("Hi ").append(userId).append(",\n\n")
.append("We confirm that your password has been successfully reset.\n\n")
.append("Please find your new login credentials below:\n\n")
.append("User ID: ").append(userId).append("\n")
.append("Temporary Password: ").append(temporaryPassword).append("\n\n")
.append("For your security, we strongly recommend that you log in using the temporary password provided ")
.append("and promptly create a new password that is unique and known only to you. ")
.append("You can change your password by accessing \"Your Profile > Change Password\" in your User Profile.\n\n")
.append("If you did not request a password reset, please contact our support team immediately at [email protected].\n\n")
.append("Best regards,\n\n")
.append("ZettaGenomics Support Team \n\n")
.toString();
}

public static void sendMail(String smtpServer, String to, String from, String subject, String body) throws Exception {

Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
// Message msg = new javax.mail.Message(session);
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.opencb.opencga.core.exceptions;

public class MailException extends Exception {

public MailException(String message) {
super(message);
}

public MailException(String message, Throwable cause) {
super(message, cause);
}

public MailException(Throwable cause) {
super(cause);
}

}

0 comments on commit 727b48e

Please sign in to comment.