-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: make MailUtils class more generic, #TASK-7192
- Loading branch information
Showing
3 changed files
with
63 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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"); | ||
|
@@ -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."); | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
opencga-core/src/main/java/org/opencb/opencga/core/exceptions/MailException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |