Skip to content

Commit

Permalink
[UPDATED]-Email properties updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ndasanayaka committed Mar 21, 2024
1 parent 5dd0c11 commit 529f7b6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
35 changes: 33 additions & 2 deletions src/net/qldarch/message/MessageContact.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.qldarch.message;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

import javax.inject.Inject;
Expand All @@ -25,6 +27,16 @@ public class MessageContact {

@Inject @Cfg("smtp.host")
private String smtpHost;
@Inject @Cfg("smtp.port")
private String smtpPort;
@Inject @Cfg("smtp.tls")
private String smtptls;
@Inject @Cfg("smtp.auth")
private String smtpAuth;
@Inject @Cfg("email.username")
private String username;
@Inject @Cfg("email.password")
private String password;

public MessageContactResponse send(String content, String senderName, String from, boolean newsletter) {
try {
Expand All @@ -33,7 +45,25 @@ public MessageContactResponse send(String content, String senderName, String fro
.replace("\n", "<br/>");
final Properties properties = new Properties();
properties.setProperty("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(properties);
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.auth", smtpAuth);
properties.setProperty("mail.smtp.starttls.enable", smtptls);
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
properties.put("mail.smtp.ssl.trust", smtpHost);

//char[] passwordArray = password.toCharArray();
Session session;

if(smtpAuth.equalsIgnoreCase("true")) {
session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}else {
session = Session.getDefaultInstance(properties);
}
MimeMessage message = new MimeMessage(session);
users.all().forEach(user -> {
if(user.isContact()) {
Expand All @@ -44,7 +74,8 @@ public MessageContactResponse send(String content, String senderName, String fro
}
}
});
message.setFrom(new InternetAddress("[email protected]"));
//message.setFrom(new InternetAddress("[email protected]"));
message.setFrom(new InternetAddress(username));
message.setSubject(subject);
message.setContent(msg, "text/html; charset=utf-8");
Transport.send(message);
Expand Down
37 changes: 35 additions & 2 deletions src/net/qldarch/security/SignUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.apache.commons.lang3.StringUtils.isBlank;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

import javax.inject.Inject;
Expand Down Expand Up @@ -39,6 +41,16 @@ public class SignUp {

@Inject @Cfg("smtp.host")
private String smtpHost;
@Inject @Cfg("smtp.port")
private String smtpPort;
@Inject @Cfg("smtp.tls")
private String smtptls;
@Inject @Cfg("smtp.auth")
private String smtpAuth;
@Inject @Cfg("email.username")
private String username;
@Inject @Cfg("email.password")
private String password;

private String activationCode() {
return rstr.next();
Expand All @@ -52,10 +64,31 @@ private void sendActivationEmail(final String email, final long id, final String
try {
final Properties properties = new Properties();
properties.setProperty("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(properties);
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.auth", smtpAuth);
properties.setProperty("mail.smtp.starttls.enable", smtptls);
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
properties.put("mail.smtp.ssl.trust", smtpHost);

//char[] passwordArray = password.toCharArray();
Session session;

if(smtpAuth.equalsIgnoreCase("true")) {
session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}else {
session = Session.getDefaultInstance(properties);
}

//Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
message.setFrom(new InternetAddress("[email protected]"));
//message.setFrom(new InternetAddress("[email protected]"));
message.setFrom(new InternetAddress(username));
message.setSubject("Qldarch Account Activation");
final String content = "Click <a href=\"%sws/account/activate?id=%s&code=%s\">here"
+ "</a> to activate your account.";
Expand Down
35 changes: 33 additions & 2 deletions src/net/qldarch/security/WsResetPassword.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.qldarch.security;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -40,6 +42,16 @@ public class WsResetPassword {
@Inject
@Cfg("smtp.host")
private String smtpHost;
@Inject @Cfg("smtp.port")
private String smtpPort;
@Inject @Cfg("smtp.tls")
private String smtptls;
@Inject @Cfg("smtp.auth")
private String smtpAuth;
@Inject @Cfg("email.username")
private String username;
@Inject @Cfg("email.password")
private String password;

private String encrypt(String s) {
final DefaultPasswordService passwordService = new DefaultPasswordService();
Expand Down Expand Up @@ -79,10 +91,29 @@ private boolean send(String content, User user) {
String msg = (content).replace("\n", "<br/>");
final Properties properties = new Properties();
properties.setProperty("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(properties);
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.auth", smtpAuth);
properties.setProperty("mail.smtp.starttls.enable", smtptls);
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
properties.put("mail.smtp.ssl.trust", smtpHost);

//char[] passwordArray = password.toCharArray();
Session session;

if(smtpAuth.equalsIgnoreCase("true")) {
session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}else {
session = Session.getDefaultInstance(properties);
}

MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setFrom(new InternetAddress("[email protected]"));
message.setFrom(new InternetAddress(username));
message.setSubject("Qldarch Password Reset");
message.setContent(msg, "text/html; charset=utf-8");
Transport.send(message);
Expand Down

0 comments on commit 529f7b6

Please sign in to comment.