Skip to content

Commit 05ad975

Browse files
authored
Merge pull request #101 from Umutayb/email-improvements
Added html support to send email!
2 parents 43c9409 + 20c044c commit 05ad975

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.umutayb</groupId>
88
<artifactId>Utilities</artifactId>
9-
<version>1.6.8</version>
9+
<version>1.6.9</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java-Utilities</name>

src/main/java/api_assured/ApiUtilities.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@SuppressWarnings("unused")
2121
public abstract class ApiUtilities extends Caller {
2222

23-
public Printer log = new Printer(this.getClass());
23+
public static Printer log = new Printer(ApiUtilities.class);
2424

2525
/**
2626
* Converts file to multipart
@@ -29,7 +29,7 @@ public abstract class ApiUtilities extends Caller {
2929
* @param name desired name for the multipart
3030
* @return returns the multipart
3131
*/
32-
public MultipartBody.Part getMultipartFromFile(File file, String name) {
32+
public static MultipartBody.Part getMultipartFromFile(File file, String name) {
3333
RequestBody body = getRequestBodyFromFile(file);
3434
log.info("Creating multipart from " + file.getName() + " file");
3535
return MultipartBody.Part.createFormData(name, file.getName(), body);
@@ -43,7 +43,7 @@ public MultipartBody.Part getMultipartFromFile(File file, String name) {
4343
* @param mediaType desired media type
4444
* @return returns the multipart
4545
*/
46-
public MultipartBody.Part getMultipartFromFile(File file, String name, String mediaType) {
46+
public static MultipartBody.Part getMultipartFromFile(File file, String name, String mediaType) {
4747
RequestBody body = getRequestBodyFromFile(file, mediaType);
4848
log.info("Creating multipart from " + file.getName() + " file");
4949
return MultipartBody.Part.createFormData(name, file.getName(), body);
@@ -55,7 +55,7 @@ public MultipartBody.Part getMultipartFromFile(File file, String name, String me
5555
* @param file target file
5656
* @return returns the RequestBody
5757
*/
58-
public RequestBody getRequestBodyFromFile(File file) {
58+
public static RequestBody getRequestBodyFromFile(File file) {
5959
String mediaType;
6060
try {
6161
mediaType = Files.probeContentType(file.toPath());
@@ -72,7 +72,7 @@ public RequestBody getRequestBodyFromFile(File file) {
7272
* @param mediaType desired media type
7373
* @return returns the RequestBody
7474
*/
75-
public RequestBody getRequestBodyFromFile(File file, String mediaType) {
75+
public static RequestBody getRequestBodyFromFile(File file, String mediaType) {
7676
log.info("Generating request body from " + file.getName() + " file");
7777
return RequestBody.create(file, MediaType.parse(mediaType));
7878
}

src/main/java/utils/FileUtilities.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ public static String getAbsolutePath(String relativePath){
8888
* @return The contents of the file as a string.
8989
*/
9090
public static String getString(String directory, String fileName) {
91-
try {return new String(Files.readAllBytes(Paths.get(directory+"/"+fileName)));}
91+
return getString(directory+"/"+fileName);
92+
}
93+
94+
/**
95+
* Returns the contents of a file as a string.
96+
*
97+
* @param directory The directory where the file is located.
98+
* @return The contents of the file as a string.
99+
*/
100+
public static String getString(String directory) {
101+
try {return new String(Files.readAllBytes(Paths.get(directory)));}
92102
catch (IOException exception){
93-
Assert.fail(StringUtilities.markup(YELLOW, fileName+" not found!"));
103+
Assert.fail(StringUtilities.markup(YELLOW, "File at '" + directory + "' not found!"));
94104
return null;
95105
}
96106
}
@@ -108,6 +118,22 @@ public static Optional<Boolean> createIfAbsent(String pathname){
108118
return Optional.empty();
109119
}
110120

121+
/**
122+
* Saves a content to a file.
123+
*
124+
* @param directory The directory where the file should be saved.
125+
* @throws RuntimeException if an exception occurs while writing the file.
126+
*/
127+
public static void saveFile(String content, String directory){
128+
try {
129+
FileWriter file = new FileWriter(directory);
130+
file.write(String.valueOf(content));
131+
file.close();
132+
}
133+
catch (Exception gamma){Assert.fail(String.valueOf(gamma));}
134+
}
135+
136+
111137
/**
112138
* Writes a string to a file.
113139
*

src/main/java/utils/email/EmailUtilities.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package utils.email;
22

33
import collections.Pair;
4+
import context.ContextStore;
45
import jakarta.mail.*;
5-
import jakarta.mail.internet.InternetAddress;
6-
import jakarta.mail.internet.MimeBodyPart;
7-
import jakarta.mail.internet.MimeMessage;
6+
import jakarta.mail.internet.*;
87
import utils.DateUtilities;
98
import utils.Printer;
109
import utils.reflection.ReflectionUtilities;
@@ -28,6 +27,7 @@ public EmailUtilities(String host) {
2827
}
2928

3029
private static final Printer log = new Printer(EmailUtilities.class);
30+
private final boolean keepLogs = Boolean.parseBoolean(ContextStore.get("keep-email-logs", "true"));
3131
private String host;
3232

3333
/**
@@ -37,11 +37,34 @@ public EmailUtilities(String host) {
3737
* @param content the content of the email
3838
* @param receiver the email address of the recipient
3939
* @param ID the username for authenticating with the SMTP server
40-
* @param Password the password for authenticating with the SMTP server
40+
* @param password the password for authenticating with the SMTP server
4141
* @param attachment the optional multipart attachment to include in the email
4242
* @return true if the email was sent successfully, false otherwise
4343
*/
44-
public Boolean sendEmail(String subject, String content, String receiver, String ID, String Password, Multipart attachment) {
44+
public Boolean sendEmail(String subject, String content, String receiver, String ID, String password, Multipart attachment) {
45+
return this.sendEmail(
46+
subject,
47+
content,
48+
"text/plain; charset=" + MimeUtility.quote("us-ascii", HeaderTokenizer.MIME),
49+
receiver,
50+
ID,
51+
password,
52+
attachment
53+
);
54+
}
55+
56+
/**
57+
* Sends an email message with an optional attachment to the specified recipient.
58+
*
59+
* @param subject the subject of the email
60+
* @param content the content of the email
61+
* @param receiver the email address of the recipient
62+
* @param ID the username for authenticating with the SMTP server
63+
* @param password the password for authenticating with the SMTP server
64+
* @param attachment the optional multipart attachment to include in the email
65+
* @return true if the email was sent successfully, false otherwise
66+
*/
67+
public Boolean sendEmail(String subject, String content, String contentType, String receiver, String ID, String password, Multipart attachment) {
4568

4669
// Get system properties
4770
Properties properties = new Properties();
@@ -55,12 +78,12 @@ public Boolean sendEmail(String subject, String content, String receiver, String
5578
// Get the Session object.// and pass username and password
5679
Session session = Session.getInstance(properties, new Authenticator() {
5780
protected PasswordAuthentication getPasswordAuthentication() {
58-
return new PasswordAuthentication(ID, Password);
81+
return new PasswordAuthentication(ID, password);
5982
}
6083
});
6184

6285
// Used to debug SMTP issues
63-
session.setDebug(true);
86+
session.setDebug(keepLogs);
6487

6588
try {
6689
// Create a default MimeMessage object.
@@ -76,13 +99,13 @@ protected PasswordAuthentication getPasswordAuthentication() {
7699
message.setSubject(subject);
77100

78101
// Now set the actual message
79-
message.setText(content + "\n");
102+
message.setContent(content, contentType);
80103
if (attachment != null)
81104
message.setContent(attachment);
82105

83-
log.info("Sending...");
106+
if (keepLogs) log.info("Sending...");
84107
Transport.send(message);// Send message
85-
log.success("Sent message successfully!");
108+
if (keepLogs) log.success("Sent message successfully!");
86109
return true;
87110
} catch (MessagingException mex) {
88111
log.error(mex.getMessage(), mex);

0 commit comments

Comments
 (0)