Skip to content

Commit

Permalink
feat: add mail sending util (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Apr 8, 2024
1 parent 3db95fe commit dc2a772
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bigboxer23</groupId>
<artifactId>utils</artifactId>
<version>2.0.28</version>
<version>2.0.29</version>

<name>utils</name>
<!-- FIXME change it to the project's website -->
Expand Down Expand Up @@ -50,6 +50,11 @@
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class RetryingCommand {
private static final Logger logger = LoggerFactory.getLogger(RetryingCommand.class);

/**
* Run a command and retry it after a pause. Can define pause length in seconds, and number of retry attempts
* Run a command and retry it after a pause. Can define pause length in seconds, and number of
* retry attempts
*
* @param command command to run
* @param identifier identifier to log on failures
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/bigboxer23/utils/mail/MailSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.bigboxer23.utils.mail;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** */
public class MailSender {

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

private static Session mailSession;

public static void sendGmail(
String toEmail, String fromEmail, String sendingPW, String subject, String body, List<File> files) {
if (fromEmail == null || sendingPW == null || toEmail == null) {
logger.info("Not sending email, not configured");
return;
}
if (mailSession == null) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
mailSession = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, sendingPW);
}
});
}
logger.info("Sending mail... " + (files != null ? files.get(0) : ""));
try {
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(fromEmail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
message.setSubject(subject);
List<MimeBodyPart> fileList = new ArrayList<>();
if (body != null) {
MimeBodyPart aMimeBodyPart = new MimeBodyPart();
aMimeBodyPart.setText(body);
fileList.add(aMimeBodyPart);
}
if (files != null) {
for (File aFile : files) {
MimeBodyPart aMimeBodyPart = new MimeBodyPart();
aMimeBodyPart.setDataHandler(new DataHandler(new FileDataSource(aFile)));
aMimeBodyPart.setFileName(aFile.getName());
fileList.add(aMimeBodyPart);
}
}
message.setContent(new MimeMultipart(fileList.toArray(new MimeBodyPart[0])));
Transport.send(message);
} catch (MessagingException e) {
logger.error("sendGmail:", e);
mailSession = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public void testExecute() throws IOException {
throw new IOException("bad");
},
"test",
0, 3);
0,
3);
fail();
} catch (IOException e) {
assertEquals(4, countHolder[0]);
Expand Down

0 comments on commit dc2a772

Please sign in to comment.