| 2822 |
chandransh |
1 |
package in.shop2020.utils;
|
|
|
2 |
|
|
|
3 |
import java.security.Security;
|
|
|
4 |
import java.util.Properties;
|
|
|
5 |
|
|
|
6 |
import javax.activation.DataHandler;
|
|
|
7 |
import javax.activation.DataSource;
|
|
|
8 |
import javax.activation.FileDataSource;
|
|
|
9 |
import javax.mail.Message;
|
|
|
10 |
import javax.mail.MessagingException;
|
|
|
11 |
import javax.mail.Multipart;
|
|
|
12 |
import javax.mail.PasswordAuthentication;
|
|
|
13 |
import javax.mail.Session;
|
|
|
14 |
import javax.mail.Transport;
|
|
|
15 |
import javax.mail.internet.InternetAddress;
|
|
|
16 |
import javax.mail.internet.MimeBodyPart;
|
|
|
17 |
import javax.mail.internet.MimeMessage;
|
|
|
18 |
import javax.mail.internet.MimeMultipart;
|
|
|
19 |
|
|
|
20 |
public class GmailUtils {
|
|
|
21 |
|
|
|
22 |
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
|
|
|
23 |
private static final String SMTP_PORT = "465";
|
|
|
24 |
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
|
|
25 |
|
|
|
26 |
public GmailUtils(){
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
|
|
|
30 |
boolean debug = true;
|
|
|
31 |
|
|
|
32 |
Properties props = new Properties();
|
|
|
33 |
props.put("mail.smtp.host", SMTP_HOST_NAME);
|
|
|
34 |
props.put("mail.smtp.auth", "true");
|
|
|
35 |
props.put("mail.debug", "true");
|
|
|
36 |
props.put("mail.smtp.port", SMTP_PORT);
|
|
|
37 |
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
|
|
|
38 |
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
|
|
|
39 |
props.put("mail.smtp.socketFactory.fallback", "false");
|
|
|
40 |
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
|
|
41 |
|
|
|
42 |
Session session = Session.getDefaultInstance(props,
|
|
|
43 |
new javax.mail.Authenticator() {
|
|
|
44 |
|
|
|
45 |
protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
46 |
return new PasswordAuthentication(from, password);
|
|
|
47 |
}
|
|
|
48 |
});
|
|
|
49 |
|
|
|
50 |
session.setDebug(debug);
|
|
|
51 |
|
|
|
52 |
Message msg = new MimeMessage(session);
|
|
|
53 |
|
|
|
54 |
//Set the from address
|
|
|
55 |
InternetAddress addressFrom = new InternetAddress(from);
|
|
|
56 |
msg.setFrom(addressFrom);
|
|
|
57 |
|
|
|
58 |
//Set the recipients
|
|
|
59 |
InternetAddress[] addressTo = new InternetAddress[recipients.length];
|
|
|
60 |
for (int i = 0; i < recipients.length; i++) {
|
|
|
61 |
addressTo[i] = new InternetAddress(recipients[i]);
|
|
|
62 |
}
|
|
|
63 |
msg.setRecipients(Message.RecipientType.TO, addressTo);
|
|
|
64 |
|
|
|
65 |
//Setting the Subject
|
|
|
66 |
msg.setSubject(subject);
|
|
|
67 |
|
|
|
68 |
//Create the multi-part object to hold the text and attachment parts
|
|
|
69 |
Multipart multipart = new MimeMultipart();
|
|
|
70 |
|
|
|
71 |
// create the message part
|
|
|
72 |
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
|
|
73 |
//Part 1: Text message
|
|
|
74 |
messageBodyPart.setText(message);
|
|
|
75 |
multipart.addBodyPart(messageBodyPart);
|
|
|
76 |
|
|
|
77 |
//Part 2: Attachment
|
|
|
78 |
messageBodyPart = new MimeBodyPart();
|
|
|
79 |
DataSource source = new FileDataSource(filename);
|
|
|
80 |
messageBodyPart.setDataHandler(new DataHandler(source));
|
|
|
81 |
String[] names = filename.split("/");
|
|
|
82 |
messageBodyPart.setFileName(names[names.length - 1]);
|
|
|
83 |
multipart.addBodyPart(messageBodyPart);
|
|
|
84 |
|
|
|
85 |
// Put parts in message
|
|
|
86 |
msg.setContent(multipart);
|
|
|
87 |
Transport.send(msg);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public static void main(String args[]) throws Exception {
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
String[] sendTo = { "chandranshu.s@shop2020.in" };
|
|
|
94 |
String emailSubjectTxt = "A test from gmail";
|
|
|
95 |
String emailMsgTxt = "Test Message Contents";
|
|
|
96 |
String emailFromAddress = "chandranshu.s@shop2020.in";
|
|
|
97 |
String password = "casa12va";
|
|
|
98 |
String filename = "/tmp/po-10.pdf";
|
|
|
99 |
|
|
|
100 |
GmailUtils utils = new GmailUtils();
|
|
|
101 |
utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, filename);
|
|
|
102 |
System.out.println("Sucessfully Sent mail to All Users");
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
}
|