| Line 1... |
Line 1... |
| 1 |
package in.shop2020.utils;
|
1 |
package in.shop2020.utils;
|
| 2 |
|
2 |
|
| 3 |
import java.io.File;
|
3 |
import java.io.File;
|
| 4 |
import java.security.Security;
|
4 |
import java.security.Security;
|
| - |
|
5 |
import java.util.ArrayList;
|
| - |
|
6 |
import java.util.HashMap;
|
| 5 |
import java.util.List;
|
7 |
import java.util.List;
|
| - |
|
8 |
import java.util.Map;
|
| 6 |
import java.util.Properties;
|
9 |
import java.util.Properties;
|
| 7 |
|
10 |
|
| 8 |
import javax.activation.DataHandler;
|
11 |
import javax.activation.DataHandler;
|
| 9 |
import javax.activation.DataSource;
|
12 |
import javax.activation.DataSource;
|
| 10 |
import javax.activation.FileDataSource;
|
13 |
import javax.activation.FileDataSource;
|
| Line 29... |
Line 32... |
| 29 |
|
32 |
|
| 30 |
public GmailUtils(){
|
33 |
public GmailUtils(){
|
| 31 |
}
|
34 |
}
|
| 32 |
|
35 |
|
| 33 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
|
36 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
|
| - |
|
37 |
try {
|
| 34 |
Message msg = prepareMessage(recipients, subject, from, password);
|
38 |
Message msg = prepareMessage(recipients, subject, from, password);
|
| - |
|
39 |
//Create the multi-part object to hold the text and attachment parts
|
| - |
|
40 |
Multipart multipart = new MimeMultipart();
|
| - |
|
41 |
// create the message part
|
| - |
|
42 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
| - |
|
43 |
//Part 1: Text message
|
| - |
|
44 |
messageBodyPart.setText(message);
|
| - |
|
45 |
multipart.addBodyPart(messageBodyPart);
|
| - |
|
46 |
//Part 2: Attachment
|
| - |
|
47 |
messageBodyPart = new MimeBodyPart();
|
| - |
|
48 |
DataSource source = new FileDataSource(filename);
|
| - |
|
49 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
| - |
|
50 |
String[] names = filename.split("/");
|
| - |
|
51 |
messageBodyPart.setFileName(names[names.length - 1]);
|
| - |
|
52 |
multipart.addBodyPart(messageBodyPart);
|
| - |
|
53 |
// Put parts in message
|
| - |
|
54 |
msg.setContent(multipart);
|
| - |
|
55 |
Transport.send(msg);
|
| - |
|
56 |
} catch (Exception e) {
|
| - |
|
57 |
// If there is any error with normal mail sending, try diving receipients across domains
|
| - |
|
58 |
if (hasMultipleDomains(recipients)) {
|
| - |
|
59 |
for (List<String> subReceipients : getReceipientsPerDomain(recipients).values()) {
|
| - |
|
60 |
sendSSLMessage(subReceipients.toArray(new String[0]), subject, message, from, password, filename);
|
| - |
|
61 |
}
|
| - |
|
62 |
|
| - |
|
63 |
return;
|
| - |
|
64 |
}
|
| - |
|
65 |
|
| - |
|
66 |
throw new MessagingException(e.getMessage(), e);
|
| - |
|
67 |
}
|
| - |
|
68 |
}
|
| 35 |
|
69 |
|
| - |
|
70 |
/**
|
| 36 |
//Create the multi-part object to hold the text and attachment parts
|
71 |
* @param recipients
|
| 37 |
Multipart multipart = new MimeMultipart();
|
72 |
* @return
|
| 38 |
|
73 |
*/
|
| - |
|
74 |
private Map<String, List<String>> getReceipientsPerDomain(String[] recipients) {
|
| - |
|
75 |
Map<String, List<String>> result = new HashMap<String, List<String>>();
|
| 39 |
// create the message part
|
76 |
for (String recipient : recipients) {
|
| 40 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
77 |
String domain = extractDomainFromEmailAddress(recipient);
|
| - |
|
78 |
|
| 41 |
//Part 1: Text message
|
79 |
if (!result.containsKey(domain)) {
|
| 42 |
messageBodyPart.setText(message);
|
80 |
result.put(domain, new ArrayList<String>());
|
| - |
|
81 |
}
|
| - |
|
82 |
|
| 43 |
multipart.addBodyPart(messageBodyPart);
|
83 |
result.get(domain).add(recipient);
|
| - |
|
84 |
}
|
| 44 |
|
85 |
|
| 45 |
//Part 2: Attachment
|
86 |
return result;
|
| - |
|
87 |
}
|
| - |
|
88 |
|
| - |
|
89 |
/**
|
| 46 |
messageBodyPart = new MimeBodyPart();
|
90 |
* @param recipients
|
| - |
|
91 |
* @return
|
| - |
|
92 |
*/
|
| 47 |
DataSource source = new FileDataSource(filename);
|
93 |
private boolean hasMultipleDomains(String[] recipients) {
|
| 48 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
94 |
String domain = extractDomainFromEmailAddress(recipients[0]);
|
| 49 |
String[] names = filename.split("/");
|
95 |
for (String recipient : recipients) {
|
| 50 |
messageBodyPart.setFileName(names[names.length - 1]);
|
96 |
if (!domain.equals(extractDomainFromEmailAddress(recipient))) {
|
| 51 |
multipart.addBodyPart(messageBodyPart);
|
97 |
return true;
|
| - |
|
98 |
}
|
| - |
|
99 |
}
|
| 52 |
|
100 |
|
| 53 |
// Put parts in message
|
101 |
return false;
|
| - |
|
102 |
}
|
| - |
|
103 |
|
| 54 |
msg.setContent(multipart);
|
104 |
private String extractDomainFromEmailAddress(String recipient) {
|
| 55 |
Transport.send(msg);
|
105 |
return recipient.split("@")[1];
|
| 56 |
}
|
106 |
}
|
| 57 |
|
107 |
|
| 58 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename, byte[] bytes, String type) throws MessagingException {
|
108 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename, byte[] bytes, String type) throws MessagingException {
|
| 59 |
Message msg = prepareMessage(recipients, subject, from, password);
|
109 |
Message msg = prepareMessage(recipients, subject, from, password);
|
| 60 |
|
110 |
|
| Line 78... |
Line 128... |
| 78 |
msg.setContent(multipart);
|
128 |
msg.setContent(multipart);
|
| 79 |
Transport.send(msg);
|
129 |
Transport.send(msg);
|
| 80 |
}
|
130 |
}
|
| 81 |
|
131 |
|
| 82 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
|
132 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
|
| - |
|
133 |
try {
|
| 83 |
Message msg = prepareMessage(recipients, subject, from, password);
|
134 |
Message msg = prepareMessage(recipients, subject, from, password);
|
| 84 |
|
135 |
|
| 85 |
//Create the multi-part object to hold the text and attachment parts
|
136 |
//Create the multi-part object to hold the text and attachment parts
|
| 86 |
Multipart multipart = new MimeMultipart();
|
137 |
Multipart multipart = new MimeMultipart();
|
| 87 |
|
138 |
|
| 88 |
// create the message part
|
139 |
// create the message part
|
| 89 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
140 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
| 90 |
//Part 1: Text message
|
141 |
//Part 1: Text message
|
| 91 |
messageBodyPart.setText(message);
|
142 |
messageBodyPart.setText(message);
|
| 92 |
multipart.addBodyPart(messageBodyPart);
|
- |
|
| 93 |
|
- |
|
| 94 |
//Part 2: Attachment
|
- |
|
| 95 |
for (File file : files) {
|
- |
|
| 96 |
DataSource source = new FileDataSource(file);
|
- |
|
| 97 |
String[] names = file.getAbsolutePath().split("/");
|
- |
|
| 98 |
messageBodyPart = new MimeBodyPart();
|
- |
|
| 99 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
- |
|
| 100 |
messageBodyPart.setFileName(names[names.length - 1]);
|
- |
|
| 101 |
multipart.addBodyPart(messageBodyPart);
|
143 |
multipart.addBodyPart(messageBodyPart);
|
| 102 |
}
|
- |
|
| 103 |
|
144 |
|
| - |
|
145 |
//Part 2: Attachment
|
| - |
|
146 |
for (File file : files) {
|
| - |
|
147 |
DataSource source = new FileDataSource(file);
|
| - |
|
148 |
String[] names = file.getAbsolutePath().split("/");
|
| - |
|
149 |
messageBodyPart = new MimeBodyPart();
|
| - |
|
150 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
| - |
|
151 |
messageBodyPart.setFileName(names[names.length - 1]);
|
| - |
|
152 |
multipart.addBodyPart(messageBodyPart);
|
| - |
|
153 |
}
|
| - |
|
154 |
|
| 104 |
// Put parts in message
|
155 |
// Put parts in message
|
| 105 |
msg.setContent(multipart);
|
156 |
msg.setContent(multipart);
|
| 106 |
Transport.send(msg);
|
157 |
Transport.send(msg);
|
| - |
|
158 |
} catch (Exception e) {
|
| - |
|
159 |
// If there is any error with normal mail sending, try diving receipients across domains
|
| - |
|
160 |
if (hasMultipleDomains(recipients)) {
|
| - |
|
161 |
for (List<String> subReceipients : getReceipientsPerDomain(recipients).values()) {
|
| - |
|
162 |
sendSSLMessage(subReceipients.toArray(new String[0]), subject, message, from, password, files);
|
| - |
|
163 |
}
|
| - |
|
164 |
|
| - |
|
165 |
return;
|
| - |
|
166 |
}
|
| - |
|
167 |
|
| - |
|
168 |
throw new MessagingException(e.getMessage(), e);
|
| - |
|
169 |
}
|
| 107 |
}
|
170 |
}
|
| 108 |
|
171 |
|
| - |
|
172 |
@SuppressWarnings("restriction")
|
| 109 |
private Message prepareMessage(String[] recipients, String subject,
|
173 |
private Message prepareMessage(String[] recipients, String subject,
|
| 110 |
final String from, final String password) throws AddressException,
|
174 |
final String from, final String password) throws AddressException,
|
| 111 |
MessagingException {
|
175 |
MessagingException {
|
| 112 |
boolean debug = true;
|
176 |
boolean debug = true;
|
| 113 |
|
177 |
|
| Line 148... |
Line 212... |
| 148 |
msg.setSubject(subject);
|
212 |
msg.setSubject(subject);
|
| 149 |
return msg;
|
213 |
return msg;
|
| 150 |
}
|
214 |
}
|
| 151 |
|
215 |
|
| 152 |
public static void main(String args[]) throws Exception {
|
216 |
public static void main(String args[]) throws Exception {
|
| 153 |
|
- |
|
| 154 |
|
- |
|
| 155 |
String[] sendTo = { "chandranshu.s@shop2020.in" };
|
217 |
String[] sendTo = { "mandeep.dhir@shop2020.in", "mandeep.dhir@gmail.com", "mandeepcse07@yahoo.co.in", "anupam.singh@shop2020.in" };
|
| 156 |
String emailSubjectTxt = "A test from gmail";
|
218 |
String emailSubjectTxt = "Another test from gmail";
|
| 157 |
String emailMsgTxt = "Test Message Contents";
|
219 |
String emailMsgTxt = "Test Message Contents";
|
| 158 |
String emailFromAddress = "chandranshu.s@shop2020.in";
|
220 |
String emailFromAddress = "mandeep.dhir@shop2020.in";
|
| 159 |
String password = "casa12va";
|
221 |
String password = "";
|
| 160 |
String filename = "/tmp/po-10.pdf";
|
222 |
String filename = "/home/mandeep/virtual_warehouse.sql";
|
| 161 |
|
223 |
|
| 162 |
GmailUtils utils = new GmailUtils();
|
224 |
GmailUtils utils = new GmailUtils();
|
| 163 |
utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, filename);
|
225 |
utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, filename);
|
| 164 |
System.out.println("Sucessfully Sent mail to All Users");
|
226 |
System.out.println("Sucessfully Sent mail to All Users");
|
| 165 |
}
|
227 |
}
|
| 166 |
|
- |
|
| 167 |
}
|
228 |
}
|