Repository navigation
Expand file tree
/
Copy pathSendMailSingle.java
More file actions
188 lines (160 loc) · 6.37 KB
/
Copy pathSendMailSingle.java
File metadata and controls
188 lines (160 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package Jvakt;
/*
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.util.*;
//import javax.mail.*;
//import javax.mail.Authenticator;
//import javax.mail.PasswordAuthentication;
import jakarta.mail.*;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
public class SendMailSingle {
// static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
// static ResultSet rs;
static Connection conn = null;
static boolean swDelete;
static boolean swTiming;
static boolean swFound;
static boolean swShDay; // set when the scheduled day is active
static boolean swDormant = false;
static java.sql.Date zDate;
static java.sql.Timestamp zD;
static java.sql.Timestamp zTs;
static java.sql.Time zT;
static Long lhou, lmin, Lsec, Lrptdat, Lchktim;
static int errors = 0;
static int warnings = 0;
static int infos = 0;
static int resolved = 0;
static String ebody = "";
static String wbody = "";
static String rbody = "";
static String tblStr = "<TABLE COLS=2 BORDER=4 cellpadding=\"3\" width=\"100%\" >";
static String tblEnd = "</TABLE>";
static String hdrStrG = "<TH BGCOLOR=\"#00FF00\"><FONT SIZE=5>"; // Green
static String hdrStrY = "<TH BGCOLOR=\"#FFFF00\"><FONT SIZE=5>"; // Yellow
static String hdrStrR = "<TH BGCOLOR=\"#FF6600\"><FONT SIZE=5>"; // Red
static String hdrStrM = "<TH BGCOLOR=\"#FF00FF\"><FONT SIZE=5>"; // Magenta
static String hdrEnd = "</TH>";
static String rowStr = "<TR>";
static String rowEnd = "</TR>";
static String boxStrG = "<TD BGCOLOR=\"#00FF00\">"; // Green
static String boxStrY = "<TD BGCOLOR=\"#FFFF00\">"; // Yellow
static String boxStrR = "<TD BGCOLOR=\"#FF6600\">"; // Red
static String boxStrM = "<TD BGCOLOR=\"#FF00FF\">"; // Magenta
static String boxStr = "<TD>";
static String boxEnd = "</TD>";
public static void main(String[] args ) throws IOException, UnknownHostException {
String version = "SendMailSingle ";
version += getVersion()+".54";
//Declare recipient's & sender's e-mail id.
final String toEmail;
final String fromEmail;
final String uname;
final String pwd;
String pwdW;
final String smtphost;
final String smtpport;
String to = null;
String subject = null;
String body = null;
String attach = null;
// String status= "default";
// String sub = "default";
String config = null;
File configF;
boolean swMail = false;
for (int i=0; i<args.length; i++) {
if (args[i].equalsIgnoreCase("-to")) to = args[++i];
else if (args[i].equalsIgnoreCase("-body")) body = args[++i];
else if (args[i].equalsIgnoreCase("-subject")) subject = args[++i];
else if (args[i].equalsIgnoreCase("-attach")) attach = args[++i];
else if (args[i].equalsIgnoreCase("-config")) config = args[++i];
}
System.out.println("----- Jvakt: "+new Date()+" Version: "+version);
System.out.println("To:"+to+" Subject:"+subject+" Body:"+body );
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
if (args.length < 1 || to == null || subject == null || body == null ) {
System.out.println("\n"+version);
System.out.println("by Michael Ekdal Perstorp Sweden.\n");
System.out.println("-to \t - Email address");
System.out.println("-subject \t - Subject");
System.out.println("-body \t - Body");
System.out.println("-attach \t - Filename (optional)");
System.out.println("-config \t - Folder of config files");
System.exit(4);
}
System.out.println("-config file: "+configF);
Properties prop = new Properties();
InputStream input = null;
// try {
// input = new FileInputStream(config + "jVakt.properties");
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
fromEmail= prop.getProperty("fromEmail");
uname = prop.getProperty("smtpuser");
pwdW = prop.getProperty("smtppwd");
if (pwdW.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(pwdW.substring(3));
String decodedString = new String(decodedBytes);
pwdW=decodedString;
}
pwd=pwdW;
smtphost = prop.getProperty("smtphost");
smtpport = prop.getProperty("smtpport");
if (fromEmail == null || uname == null || pwd == null || smtphost == null || smtpport == null ) {
System.out.println("==> fromEmail, smtpuser, smtppwd,smtphost and smtpport must be present in the Jvakt.properties file! <==\n");
System.exit(4);
}
int smtpporti = Integer.parseInt(smtpport);
String mode = prop.getProperty("mode");
if (!mode.equalsIgnoreCase("active")) swDormant = true;
input.close();
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, pwd);
}
};
//Set properties and their values
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.port", smtpporti);
swMail = true;
// subject = sub;
toEmail = to;
// System.out.println("CheckStatus Severe: " + errors + " Problems: " + warnings + " Info: " + infos );
System.out.println("\nTo: " + toEmail );
System.out.println("From: " + fromEmail );
System.out.println("Subject: " + subject );
System.out.println("Body: " + body + "\n");
if (swMail && !swDormant) {
Session session = Session.getInstance(props, auth);
if (attach == null) EmailUtil.sendEmail(session, toEmail,subject, body, fromEmail);
else EmailUtil.sendEmail(session, toEmail,subject, body, fromEmail, attach);
}
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
}