| Line 25... |
Line 25... |
| 25 |
import javax.mail.internet.MimeBodyPart;
|
25 |
import javax.mail.internet.MimeBodyPart;
|
| 26 |
import javax.mail.internet.MimeMessage;
|
26 |
import javax.mail.internet.MimeMessage;
|
| 27 |
import javax.mail.internet.MimeMultipart;
|
27 |
import javax.mail.internet.MimeMultipart;
|
| 28 |
|
28 |
|
| 29 |
public class EmailUtils {
|
29 |
public class EmailUtils {
|
| 30 |
public static void main(String... args) throws IOException{
|
30 |
public static void main(String... args) throws IOException, MessagingException{
|
| 31 |
|
31 |
|
| 32 |
System.out.println("Test Email Start");
|
32 |
System.out.println("Test Email Start");
|
| 33 |
String[] sendTo = { "vikram.raghav@shop2020.in"};
|
33 |
String[] sendTo = { "vikram.raghav@shop2020.in"};
|
| 34 |
EmailUtils emailUtils = new EmailUtils();
|
34 |
EmailUtils emailUtils = new EmailUtils();
|
| 35 |
emailUtils.sendEmail(sendTo,"------DONT PANIC--------TEST EMAIL--------","MAIL Body","build@shop2020.in","/home/Mobiles-Tablets-FBA-Sale.xls","/home/Mobiles-Tablets-FBA-body.html");
|
35 |
emailUtils.sendEmail(sendTo,"------DONT PANIC--------TEST EMAIL--------","MAIL Body","build@shop2020.in","/home/Mobiles-Tablets-FBA-Sale.xls","/home/Mobiles-Tablets-FBA-body.html");
|
| Line 42... |
Line 42... |
| 42 |
* @param session
|
42 |
* @param session
|
| 43 |
* @param toEmail
|
43 |
* @param toEmail
|
| 44 |
* @param subject
|
44 |
* @param subject
|
| 45 |
* @param body
|
45 |
* @param body
|
| 46 |
*/
|
46 |
*/
|
| 47 |
public void sendEmail(String[] recipients, String subject, String body,String fromEmail,String filename,String htmlFileName){
|
47 |
public void sendEmail(String recipients[], String subject, String body, String from, String filename,String htmlFileName) throws MessagingException {
|
| 48 |
try {
|
48 |
try {
|
| - |
|
49 |
BufferedReader bufferedReader = new BufferedReader(new FileReader(htmlFileName));
|
| - |
|
50 |
|
| - |
|
51 |
StringBuffer htmlContent = new StringBuffer();
|
| - |
|
52 |
String line = null;
|
| - |
|
53 |
|
| - |
|
54 |
while((line =bufferedReader.readLine())!=null){
|
| - |
|
55 |
|
| - |
|
56 |
htmlContent.append(line).append("\n");
|
| - |
|
57 |
|
| - |
|
58 |
}
|
| - |
|
59 |
|
| 49 |
Message msg = prepareMessage(recipients, subject, fromEmail);
|
60 |
Message msg = prepareMessage(recipients, subject, from);
|
| 50 |
//Create the multi-part object to hold the text and attachment parts
|
61 |
//Create the multi-part object to hold the text and attachment parts
|
| 51 |
Multipart multipart = new MimeMultipart();
|
62 |
Multipart multipart = new MimeMultipart();
|
| - |
|
63 |
|
| - |
|
64 |
MimeBodyPart htmlPart = new MimeBodyPart();
|
| - |
|
65 |
|
| - |
|
66 |
htmlPart.setContent(htmlContent.toString(), "text/html; charset=utf-8");
|
| - |
|
67 |
|
| 52 |
// create the message part
|
68 |
// create the message part
|
| 53 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
69 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
| 54 |
//Part 1: Text message
|
70 |
//Part 1: Text message
|
| 55 |
messageBodyPart.setText(body);
|
71 |
messageBodyPart.setText(body);
|
| 56 |
multipart.addBodyPart(messageBodyPart);
|
72 |
multipart.addBodyPart(htmlPart);
|
| 57 |
//Part 2: Attachment
|
73 |
//Part 2: Attachment
|
| 58 |
messageBodyPart = new MimeBodyPart();
|
74 |
messageBodyPart = new MimeBodyPart();
|
| 59 |
DataSource source = new FileDataSource(filename);
|
75 |
DataSource source = new FileDataSource(filename);
|
| 60 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
76 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
| 61 |
String[] names = filename.split("/");
|
77 |
String[] names = filename.split("/");
|
| Line 63... |
Line 79... |
| 63 |
multipart.addBodyPart(messageBodyPart);
|
79 |
multipart.addBodyPart(messageBodyPart);
|
| 64 |
// Put parts in message
|
80 |
// Put parts in message
|
| 65 |
msg.setContent(multipart);
|
81 |
msg.setContent(multipart);
|
| 66 |
Transport.send(msg);
|
82 |
Transport.send(msg);
|
| 67 |
} catch (Exception e) {
|
83 |
} catch (Exception e) {
|
| 68 |
System.out.println("Cant send email");
|
- |
|
| 69 |
}
|
84 |
}
|
| 70 |
|
- |
|
| 71 |
}
|
85 |
}
|
| 72 |
private Message prepareMessage(String[] recipients, String subject,
|
86 |
private Message prepareMessage(String[] recipients, String subject,
|
| 73 |
final String from) throws AddressException,
|
87 |
final String from) throws AddressException,
|
| 74 |
MessagingException {
|
88 |
MessagingException {
|
| 75 |
Properties props = new Properties();
|
89 |
Properties props = new Properties();
|
| Line 103... |
Line 117... |
| 103 |
}
|
117 |
}
|
| 104 |
|
118 |
|
| 105 |
private String extractDomainFromEmailAddress(String recipient) {
|
119 |
private String extractDomainFromEmailAddress(String recipient) {
|
| 106 |
return recipient.split("@")[1];
|
120 |
return recipient.split("@")[1];
|
| 107 |
}
|
121 |
}
|
| 108 |
|
122 |
|
| 109 |
private Map<String, List<String>> getReceipientsPerDomain(String[] recipients) {
|
123 |
private Map<String, List<String>> getReceipientsPerDomain(String[] recipients) {
|
| 110 |
Map<String, List<String>> result = new HashMap<String, List<String>>();
|
124 |
Map<String, List<String>> result = new HashMap<String, List<String>>();
|
| 111 |
for (String recipient : recipients) {
|
125 |
for (String recipient : recipients) {
|
| 112 |
String domain = extractDomainFromEmailAddress(recipient);
|
126 |
String domain = extractDomainFromEmailAddress(recipient);
|
| 113 |
|
127 |
|