11package utils .email ;
22
33import collections .Pair ;
4+ import context .ContextStore ;
45import 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 .*;
87import utils .DateUtilities ;
98import utils .Printer ;
109import 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