Skip to content

Commit

Permalink
Upload to email libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
C4J committed Oct 10, 2023
1 parent a7974a5 commit 427cbc2
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 88 deletions.
7 changes: 4 additions & 3 deletions b6sftpSend/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
<classpathentry kind="lib" path="lib/devonly/i4jruntime.jar"/>
<classpathentry kind="lib" path="lib/app/commons-io-2.13.0.jar"/>
<classpathentry kind="lib" path="lib/app/commons-codec-1.15.jar"/>
<classpathentry kind="lib" path="lib/mail/commons-email-1.5.jar"/>
<classpathentry kind="lib" path="lib/mail/javax.mail.jar"/>
<classpathentry kind="lib" path="lib/mail/javax.activation-1.2.0.jar"/>
<classpathentry kind="lib" path="lib/app/slf4j-jdk14-2.0.7.jar"/>
<classpathentry kind="lib" path="lib/mail/angus-activation-2.0.1.jar"/>
<classpathentry kind="lib" path="lib/mail/angus-mail-2.0.2.jar"/>
<classpathentry kind="lib" path="lib/mail/jakarta.activation-api-2.1.2.jar"/>
<classpathentry kind="lib" path="lib/mail/jakarta.mail-api-2.1.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
7 changes: 4 additions & 3 deletions b6sftpSend/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Class-Path: lib/app/asn-one-0.6.0.jar
lib/app/slf4j-jdk14-2.0.7.jar
lib/app/slf4j-simple-2.0.7.jar
lib/app/sshj-0.35.0.jar
lib/mail/commons-email-1.5.jar
lib/mail/javax.activation-1.2.0.jar
lib/mail/javax.mail.jar
lib/mail/angus-activation-2.0.1.jar
lib/mail/angus-mail-2.0.2.jar
lib/mail/jakarta.activation-api-2.1.2.jar
lib/mail/jakarta.mail-api-2.1.2.jar
sftpSend.jar
4 changes: 2 additions & 2 deletions b6sftpSend/b6sftpSend.install4j
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<install4j version="10.0.6" transformSequenceNumber="10">
<directoryPresets config="./Images/icons/SFTP_Send_Icons" />
<application name="SFTP Send" applicationId="1389-2676-6857-5998" mediaDir="../../../Distribution/java17/b6sftpSend" shortName="sftpSend" publisher="David Garratt" publisherWeb="https://www.commander4j.com" version="4.01" allPathsRelative="true" macVolumeId="7e8a68a2fd94926b" javaMinVersion="17" javaMaxVersion="17">
<jreBundles jdkProviderId="Zulu" release="17/17.0.7" />
<application name="SFTP Send" applicationId="1389-2676-6857-5998" mediaDir="../../../Distribution/java17/b6sftpSend" shortName="sftpSend" publisher="David Garratt" publisherWeb="https://www.commander4j.com" version="4.10" allPathsRelative="true" macVolumeId="7e8a68a2fd94926b" javaMinVersion="17" javaMaxVersion="17">
<jreBundles jdkProviderId="Zulu" release="17/17.0.8.1" />
</application>
<files globalExcludeSuffixes="dirClean.ok,.DS_Store">
<mountPoints>
Expand Down
Binary file not shown.
Binary file modified b6sftpSend/bin/com/commander4j/email/SendEmail.class
Binary file not shown.
Binary file modified b6sftpSend/bin/com/commander4j/sftp/Transfer.class
Binary file not shown.
Binary file modified b6sftpSend/bin/com/commander4j/util/JUtility.class
Binary file not shown.
18 changes: 10 additions & 8 deletions b6sftpSend/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
<pathelement location="lib/app/log4j-core-2.20.0.jar"/>
<pathelement location="lib/app/log4j-slf4j2-impl-2.20.0.jar"/>
<pathelement location="lib/app/slf4j-api-2.0.7.jar"/>
<pathelement location="lib/app/slf4j-jdk14-2.0.7.jar"/>
<pathelement location="lib/app/slf4j-simple-2.0.7.jar"/>
<pathelement location="lib/app/sshj-0.35.0.jar"/>
<pathelement location="lib/devonly/i4jruntime.jar"/>
<pathelement location="lib/app/commons-io-2.13.0.jar"/>
<pathelement location="lib/app/commons-codec-1.15.jar"/>
<pathelement location="lib/mail/commons-email-1.5.jar"/>
<pathelement location="lib/mail/javax.mail.jar"/>
<pathelement location="lib/mail/javax.activation-1.2.0.jar"/>
<pathelement location="lib/app/slf4j-jdk14-2.0.7.jar"/>
<pathelement location="lib/mail/angus-activation-2.0.1.jar"/>
<pathelement location="lib/mail/angus-mail-2.0.2.jar"/>
<pathelement location="lib/mail/jakarta.activation-api-2.1.2.jar"/>
<pathelement location="lib/mail/jakarta.mail-api-2.1.2.jar"/>
</path>
<path id="run.Transfer.classpath">
<path refid="b6sftpSend.classpath"/>
Expand All @@ -40,15 +41,16 @@
<pathelement location="lib/app/log4j-core-2.20.0.jar"/>
<pathelement location="lib/app/log4j-slf4j2-impl-2.20.0.jar"/>
<pathelement location="lib/app/slf4j-api-2.0.7.jar"/>
<pathelement location="lib/app/slf4j-jdk14-2.0.7.jar"/>
<pathelement location="lib/app/slf4j-simple-2.0.7.jar"/>
<pathelement location="lib/app/sshj-0.35.0.jar"/>
<pathelement location="lib/devonly/i4jruntime.jar"/>
<pathelement location="lib/app/commons-io-2.13.0.jar"/>
<pathelement location="lib/app/commons-codec-1.15.jar"/>
<pathelement location="lib/mail/commons-email-1.5.jar"/>
<pathelement location="lib/mail/javax.mail.jar"/>
<pathelement location="lib/mail/javax.activation-1.2.0.jar"/>
<pathelement location="lib/app/slf4j-jdk14-2.0.7.jar"/>
<pathelement location="lib/mail/angus-activation-2.0.1.jar"/>
<pathelement location="lib/mail/angus-mail-2.0.2.jar"/>
<pathelement location="lib/mail/jakarta.activation-api-2.1.2.jar"/>
<pathelement location="lib/mail/jakarta.mail-api-2.1.2.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
Expand Down
Binary file added b6sftpSend/lib/mail/angus-activation-2.0.1.jar
Binary file not shown.
Binary file added b6sftpSend/lib/mail/angus-mail-2.0.2.jar
Binary file not shown.
Binary file removed b6sftpSend/lib/mail/commons-email-1.5.jar
Binary file not shown.
Binary file not shown.
Binary file added b6sftpSend/lib/mail/jakarta.mail-api-2.1.2.jar
Binary file not shown.
Binary file removed b6sftpSend/lib/mail/javax.activation-1.2.0.jar
Binary file not shown.
Binary file removed b6sftpSend/lib/mail/javax.mail.jar
Binary file not shown.
Binary file modified b6sftpSend/sftpSend.jar
Binary file not shown.
153 changes: 82 additions & 71 deletions b6sftpSend/src/com/commander4j/email/SendEmail.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
import java.util.HashMap;
import java.util.Properties;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.logging.log4j.Logger;

import com.commander4j.util.JUtility;
import com.commander4j.xml.JXMLDocument;

import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import jakarta.mail.Multipart;

public class SendEmail
{
Expand All @@ -21,7 +28,7 @@ public class SendEmail
private Properties smtpProperties;
private HashMap<String, distributionList> distList = new HashMap<String, distributionList>();
private HashMap<String, Calendar> emailLog = new HashMap<String, Calendar>();
private JUtility utils = new JUtility();
private JUtility util = new JUtility();

private boolean enabled = false;

Expand Down Expand Up @@ -64,9 +71,9 @@ public void init(String distributionID)

String addressList = "";
String enabled = "";
addressList = utils.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/toAddressList").trim());
String temp = utils.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/@maxFrequencyMins").trim());
enabled = utils.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/@enabled").trim());
addressList = util.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/toAddressList").trim());
String temp = util.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/@maxFrequencyMins").trim());
enabled = util.replaceNullStringwithBlank(doc.findXPath("//emailSettings/distributionList[@id='" + distributionID + "'][@enabled='Y']/@enabled").trim());

if (temp.equals(""))
temp = "0";
Expand Down Expand Up @@ -106,116 +113,120 @@ public synchronized boolean Send(String distributionID, String subject, String m
String emailKey = "[" + distributionID + "] - [" + subject + "]";
logger.info(emailKey);

Calendar lastSent;
Calendar lastSent = Calendar.getInstance();
Calendar now = Calendar.getInstance();

Boolean okToSend = false;

if (emailLog.containsKey(emailKey))
{
lastSent = emailLog.get(emailKey);
}
else
{
okToSend = true;
lastSent = now;
lastSent.add(Calendar.DATE, -1);
emailLog.put(emailKey, lastSent);
}

long seconds = (now.getTimeInMillis() - lastSent.getTimeInMillis()) / 1000;

long ageInMins = seconds / 60;

if (ageInMins >= distList.get(distributionID).maxFrequencyMins)
{
okToSend = true;
emailLog.put(emailKey, now);
logger.debug("Last email to " + emailKey + " was at " + util.getISODateStringFromCalendar(lastSent));
logger.debug("Current time is " + util.getISODateStringFromCalendar(now));

}
else
logger.debug("Minutes since last email to " + emailKey + " is " + String.valueOf(ageInMins));

if (ageInMins >= distList.get(distributionID).maxFrequencyMins)
{

}
emailLog.put(emailKey, now);
logger.debug("Email frequency permitted.");

if (okToSend)
{
EmailAttachment attachment = new EmailAttachment();
MultiPartEmail email = new MultiPartEmail();
try
{

Properties propAuth = new Properties();
Properties propNoAuth = new Properties();

propAuth.putAll(smtpProperties);
propNoAuth.putAll(smtpProperties);

Session authenticatedSession = Session.getInstance(propAuth, new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtpProperties.get("mail.smtp.user").toString(), smtpProperties.get("mail.smtp.password").toString());
}
});

propNoAuth.put("mail.smtp.user","");
propNoAuth.put("mail.smtp.password","");


Session unauthenticatedSession = Session.getInstance(propAuth,null);

MimeMessage message;

if (smtpProperties.get("mail.smtp.auth").toString().toLowerCase().equals("true"))
{
logger.info("Email authentication required");
email.setAuthenticator(new DefaultAuthenticator(smtpProperties.get("mail.smtp.user").toString(), smtpProperties.get("mail.smtp.password").toString()));
email.setStartTLSEnabled(true);
logger.debug("Email authentication required");
message = new MimeMessage(authenticatedSession);
}
else
{
logger.info("Email No Authentication specified");
logger.debug("Email no authentication required");
message = new MimeMessage(unauthenticatedSession);
}

email.getMailSession().getProperties().putAll(smtpProperties);

String emails = distList.get(distributionID).addressList;
String[] emailArray = emails.split(",");
emails = null;

if (emailArray.length > 0)
{
logger.debug("Email To: " + emails);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emails));

for (int x = 0; x < emailArray.length; x++)
{
email.addTo(emailArray[x].toLowerCase(), "");
logger.info("Email To: " + emailArray[x].toLowerCase());
}

emailArray = null;

try
{
message.setFrom(new InternetAddress(smtpProperties.get("mail.smtp.from").toString()));

email.setFrom(smtpProperties.get("mail.smtp.from").toString(), "");
email.setSubject(subject);
email.setMsg(messageText+"\n\n\n\nEmail from sftpSend running on ["+utils.getClientName()+"] distribution list ["+distributionID+"]");
message.setSubject(subject);

// add the attachment
if (filename.equals("") == false)
{
logger.info("Email add attachment [" + utils.getFilenameFromPath(filename) + "]");
MimeBodyPart mimeBodyPart = new MimeBodyPart();

attachment.setPath(filename);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(filename);
attachment.setName(utils.getFilenameFromPath(filename));
email.attach(attachment);
}
// mimeBodyPart.setContent(messageText, "text/html;
// charset=utf-8");
mimeBodyPart.setText(messageText, "utf-8");

// send the email
logger.info("Email begin send...");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);

email.send();
if (filename.equals("") == false)
{
logger.debug("Email add attachment [" + util.getFilenameFromPath(filename) + "]");

logger.info("Email sent successfully");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.attachFile(new File(filename));
attachmentBodyPart.setDescription(filename);

}
catch (Exception mex)
{
logger.info("Error sending email : " + mex.getMessage());
result = false;
}
multipart.addBodyPart(attachmentBodyPart);

}
message.setContent(multipart);

logger.debug("Sending email");
Transport.send(message);
logger.debug("Email sent");

message = null;
}
catch (Exception mex)
catch (Exception ex)
{
logger.info("Error sending email : " + mex.getMessage());
result = false;
logger.error("Error encountered sending email [" + ex.getMessage() + "]");
}

attachment = null;
email = null;
}
else
{
// okToSend = false;
logger.debug("Email suppressed - too frequent");
}

}
else
{
Expand Down
2 changes: 1 addition & 1 deletion b6sftpSend/src/com/commander4j/sftp/Transfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Transfer extends Thread
public static JUtility utils = new JUtility();
public static EmailQueue emailqueue = new EmailQueue();
public static EmailThread emailthread;
public static String version = "4.01";
public static String version = "4.10";
public static Long pollFrequencySeconds = (long) 0;

public static void main(String[] args)
Expand Down
13 changes: 13 additions & 0 deletions b6sftpSend/src/com/commander4j/util/JUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class JUtility
{
Expand All @@ -17,6 +19,17 @@ public String replaceNullObjectwithBlank(Object value)

return result;
}

public String getISODateStringFromCalendar(Calendar cal)
{
String result = "";

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

result = format1.format(cal.getTime());

return result;
}

public String replaceNullStringwithBlank(String value)
{
Expand Down
9 changes: 9 additions & 0 deletions b6sftpSend/xml/config/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.apache.http" level="WARN">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.http.wire" level="WARN">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.http.impl.conn" level="WARN">
<AppenderRef ref="Console"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
Expand Down

0 comments on commit 427cbc2

Please sign in to comment.