| Line 8... |
Line 8... |
| 8 |
|
8 |
|
| 9 |
import javax.activation.DataHandler;
|
9 |
import javax.activation.DataHandler;
|
| 10 |
import javax.activation.DataSource;
|
10 |
import javax.activation.DataSource;
|
| 11 |
import javax.activation.FileDataSource;
|
11 |
import javax.activation.FileDataSource;
|
| 12 |
import javax.mail.Message;
|
12 |
import javax.mail.Message;
|
| - |
|
13 |
import javax.mail.MessagingException;
|
| 13 |
import javax.mail.Multipart;
|
14 |
import javax.mail.Multipart;
|
| 14 |
import javax.mail.Session;
|
15 |
import javax.mail.Session;
|
| 15 |
import javax.mail.Transport;
|
16 |
import javax.mail.Transport;
|
| 16 |
import javax.mail.internet.InternetAddress;
|
17 |
import javax.mail.internet.InternetAddress;
|
| 17 |
import javax.mail.internet.MimeBodyPart;
|
18 |
import javax.mail.internet.MimeBodyPart;
|
| Line 21... |
Line 22... |
| 21 |
public class EmailUtils {
|
22 |
public class EmailUtils {
|
| 22 |
public static void main(String... args) throws IOException{
|
23 |
public static void main(String... args) throws IOException{
|
| 23 |
|
24 |
|
| 24 |
System.out.println("Test Email Start");
|
25 |
System.out.println("Test Email Start");
|
| 25 |
String[] sendTo = { "vikram.raghav@shop2020.in"};
|
26 |
String[] sendTo = { "vikram.raghav@shop2020.in"};
|
| 26 |
FileReader fileReader = new FileReader("/home/Mobiles-Tablets-FBA-body.html");
|
- |
|
| 27 |
StringBuffer sb = new StringBuffer();
|
- |
|
| 28 |
while(fileReader.read()!=-1){
|
- |
|
| 29 |
char c = (char) fileReader.read();
|
- |
|
| 30 |
sb.append(c);
|
- |
|
| 31 |
}
|
- |
|
| 32 |
EmailUtils.sendEmail(sendTo,"------DONT PANIC--------TEST EMAIL--------", sb.toString(),"/home/Mobiles-Tablets-FBA-Sale.xls");
|
27 |
EmailUtils.sendEmail(sendTo,"------DONT PANIC--------TEST EMAIL--------","MAIL Body","build@shop2020.in","/home/Mobiles-Tablets-FBA-Sale.xls","/home/Mobiles-Tablets-FBA-body.html");
|
| 33 |
|
- |
|
| 34 |
System.out.println("Test Email Sent");
|
28 |
System.out.println("Test Email Sent");
|
| 35 |
|
29 |
|
| 36 |
}
|
30 |
}
|
| 37 |
|
31 |
|
| 38 |
/**
|
32 |
/**
|
| Line 40... |
Line 34... |
| 40 |
* @param session
|
34 |
* @param session
|
| 41 |
* @param toEmail
|
35 |
* @param toEmail
|
| 42 |
* @param subject
|
36 |
* @param subject
|
| 43 |
* @param body
|
37 |
* @param body
|
| 44 |
*/
|
38 |
*/
|
| 45 |
public static void sendEmail(String[] toEmail, String subject, String body,String filename){
|
39 |
public static void sendEmail(String[] toEmail, String subject, String body,String fromEmail,String filename,String htmlFileName){
|
| 46 |
try
|
40 |
try
|
| 47 |
{
|
41 |
{
|
| - |
|
42 |
BufferedReader bufferedReader = new BufferedReader(new FileReader(htmlFileName));
|
| - |
|
43 |
StringBuffer htmlContent = new StringBuffer();
|
| - |
|
44 |
String line = null;
|
| - |
|
45 |
while((line =bufferedReader.readLine())!=null){
|
| - |
|
46 |
htmlContent.append(line).append("\n");
|
| - |
|
47 |
}
|
| - |
|
48 |
Multipart multipart = new MimeMultipart();
|
| - |
|
49 |
MimeBodyPart htmlPart = new MimeBodyPart();
|
| - |
|
50 |
htmlPart.setContent(htmlContent.toString(), "text/html; charset=utf-8");
|
| - |
|
51 |
multipart.addBodyPart(htmlPart);
|
| 48 |
Properties props = System.getProperties();
|
52 |
Properties props = System.getProperties();
|
| 49 |
String smtpHostServer = "localhost";
|
53 |
String smtpHostServer = "localhost";
|
| 50 |
props.put("mail.smtp.host", smtpHostServer);
|
54 |
props.put("mail.smtp.host", smtpHostServer);
|
| 51 |
Session session = Session.getInstance(props, null);
|
55 |
Session session = Session.getInstance(props, null);
|
| 52 |
MimeMessage msg = new MimeMessage(session);
|
56 |
MimeMessage msg = new MimeMessage(session);
|
| 53 |
//set message headers
|
57 |
//set message headers
|
| 54 |
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
|
58 |
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
|
| 55 |
msg.addHeader("format", "flowed");
|
59 |
msg.addHeader("format", "flowed");
|
| 56 |
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
60 |
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
| 57 |
|
61 |
|
| 58 |
msg.setFrom(new InternetAddress("build@shop2020.in", ""));
|
62 |
msg.setFrom(new InternetAddress(fromEmail, ""));
|
| 59 |
|
63 |
|
| 60 |
msg.setSubject(subject, "UTF-8");
|
64 |
msg.setSubject(subject, "UTF-8");
|
| 61 |
|
65 |
|
| 62 |
msg.setText(body, "UTF-8");
|
66 |
msg.setText(body, "UTF-8");
|
| 63 |
|
67 |
|
| 64 |
Multipart multipart = new MimeMultipart();
|
- |
|
| 65 |
// create the message part
|
68 |
// create the message part
|
| 66 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
69 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
| 67 |
//Part 1: Text message
|
70 |
//Part 1: Text message
|
| 68 |
messageBodyPart.setText(body);
|
71 |
messageBodyPart.setText(body);
|
| 69 |
multipart.addBodyPart(messageBodyPart);
|
72 |
multipart.addBodyPart(messageBodyPart);
|
| Line 72... |
Line 75... |
| 72 |
DataSource source = new FileDataSource(filename);
|
75 |
DataSource source = new FileDataSource(filename);
|
| 73 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
76 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
| 74 |
String[] names = filename.split("/");
|
77 |
String[] names = filename.split("/");
|
| 75 |
messageBodyPart.setFileName(names[names.length - 1]);
|
78 |
messageBodyPart.setFileName(names[names.length - 1]);
|
| 76 |
multipart.addBodyPart(messageBodyPart);
|
79 |
multipart.addBodyPart(messageBodyPart);
|
| 77 |
|
- |
|
| 78 |
msg.setSentDate(new Date());
|
80 |
msg.setSentDate(new Date());
|
| 79 |
for(String email:toEmail){
|
81 |
for(String email:toEmail){
|
| 80 |
msg.addRecipient(Message.RecipientType.TO,InternetAddress.parse(email)[0]);
|
82 |
msg.addRecipient(Message.RecipientType.TO,InternetAddress.parse(email)[0]);
|
| 81 |
}
|
83 |
}
|
| 82 |
System.out.println("Message is ready");
|
84 |
System.out.println("Message is ready");
|