-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailutil.txt
136 lines (116 loc) · 4.76 KB
/
mailutil.txt
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
package lib.mail.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailUtil
{
static Properties commonProperties = PropertiesFileReader.getInstance().readProperties("common.properties");
public static void zipFolder(String mstrsrcFolder, String mstrzipfolder)
throws Exception
{
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(mstrzipfolder);
zip = new ZipOutputStream(fileWriter);
String mstrpath = "";
addFolderToZip(mstrpath, mstrsrcFolder, zip);
zip.flush();
zip.close();
}
public static void addFolderToZip(String mstrpath, String mstrsrcFolder, ZipOutputStream zip)
throws Exception
{
File folder = new File(mstrsrcFolder);
for (String fileName : folder.list())
{
System.out.println("path :" + mstrpath);
if (mstrpath.equals("")) {
addFileToZip(folder.getName(), mstrsrcFolder + "/" + fileName, zip);
} else {
addFileToZip(mstrpath + "/" + folder.getName(), mstrsrcFolder + "/" + fileName, zip);
}
}
}
public static void addFileToZip(String mstrpath, String mstrsrcFile, ZipOutputStream zip)
throws Exception
{
File folder = new File(mstrsrcFile);
if (folder.isDirectory())
{
addFolderToZip(mstrpath, mstrsrcFile, zip);
}
else
{
byte[] byt = new byte[1024];
FileInputStream in = new FileInputStream(mstrsrcFile);
zip.putNextEntry(new ZipEntry(mstrpath + "/" + folder.getName()));
int intlength;
while ((intlength = in.read(byt)) > 0)
{
int intlength;
zip.write(byt, 0, intlength);
}
}
}
public static void emailNotification(String mstrzipfolder)
throws IOException
{
commonProperties.getProperty("mailFrom").trim();
String mstrEmailId_From = commonProperties.getProperty("mailFrom").trim();
String[] mstrEmailId_To = { commonProperties.getProperty("mailTo").trim() };
String[] mstrEmailId_Cc = { commonProperties.getProperty("mailCc").trim() };
String mstrEmailId_Subject = "Automation Execution Report";
String mstrEmail_Body = "Hi Team, \nPlease Find the Attached Automation Execution Report. \n\nThanks & Regards \n Divya Jain \n \n\n \nNote : This is an Auto Generated e-mail Please do not Reply.";
String mstrEmail_Host = "xchcasha.test.com";
String mstrAttach_File1 = mstrzipfolder;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", mstrEmail_Host);
Session session = Session.getDefaultInstance(properties);
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mstrEmailId_From));
InternetAddress[] addressTo = new InternetAddress[mstrEmailId_To.length];
for (int intEmailId_To = 0; intEmailId_To < mstrEmailId_To.length; intEmailId_To++) {
addressTo[intEmailId_To] = new InternetAddress(mstrEmailId_To[intEmailId_To]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
InternetAddress[] addressCc = new InternetAddress[mstrEmailId_Cc.length];
for (int intEmailId_Cc = 0; intEmailId_Cc < mstrEmailId_Cc.length; intEmailId_Cc++) {
addressCc[intEmailId_Cc] = new InternetAddress(mstrEmailId_Cc[intEmailId_Cc]);
}
message.setRecipients(Message.RecipientType.CC, addressCc);
message.setSubject(mstrEmailId_Subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mstrEmail_Body);
MimeBodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.attachFile(mstrAttach_File1);
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart1);
message.setContent(multipart);
System.out.println("Sending Message......");
Transport.send(message);
System.out.println("Message Sent Successfully....");
}
catch (MessagingException e)
{
e.printStackTrace();
System.out.println("Please Check the Email Notification Function");
}
}
}