Subversion Repositories SmartDukaan

Rev

Rev 4827 | Rev 5138 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2822 chandransh 1
package in.shop2020.utils;
2
 
4827 mandeep.dh 3
import java.io.File;
2822 chandransh 4
import java.security.Security;
4827 mandeep.dh 5
import java.util.List;
2822 chandransh 6
import java.util.Properties;
7
 
8
import javax.activation.DataHandler;
9
import javax.activation.DataSource;
10
import javax.activation.FileDataSource;
11
import javax.mail.Message;
12
import javax.mail.MessagingException;
13
import javax.mail.Multipart;
14
import javax.mail.PasswordAuthentication;
15
import javax.mail.Session;
16
import javax.mail.Transport;
4827 mandeep.dh 17
import javax.mail.internet.AddressException;
2822 chandransh 18
import javax.mail.internet.InternetAddress;
19
import javax.mail.internet.MimeBodyPart;
20
import javax.mail.internet.MimeMessage;
21
import javax.mail.internet.MimeMultipart;
5128 amit.gupta 22
import javax.mail.util.ByteArrayDataSource;
2822 chandransh 23
 
24
public class GmailUtils {
25
 
26
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
27
    private static final String SMTP_PORT = "465";
28
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
29
 
30
    public GmailUtils(){
31
    }
32
 
33
    public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
4827 mandeep.dh 34
        Message msg = prepareMessage(recipients, subject, from, password);
35
 
36
        //Create the multi-part object to hold the text and attachment parts
37
        Multipart multipart = new MimeMultipart();
38
 
39
        // create the message part 
40
        MimeBodyPart messageBodyPart = new MimeBodyPart();
41
        //Part 1: Text message
42
        messageBodyPart.setText(message);
43
        multipart.addBodyPart(messageBodyPart);
44
 
45
        //Part 2: Attachment
46
        messageBodyPart = new MimeBodyPart();
47
        DataSource source = new FileDataSource(filename);
48
        messageBodyPart.setDataHandler(new DataHandler(source));
49
        String[] names = filename.split("/");
50
        messageBodyPart.setFileName(names[names.length - 1]);
51
        multipart.addBodyPart(messageBodyPart);
52
 
53
        // Put parts in message
54
        msg.setContent(multipart);
55
        Transport.send(msg);
56
    }
57
 
5128 amit.gupta 58
    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);
60
 
61
    	//Create the multi-part object to hold the text and attachment parts
62
    	Multipart multipart = new MimeMultipart();
63
 
64
    	// create the message part 
65
    	MimeBodyPart messageBodyPart = new MimeBodyPart();
66
    	//Part 1: Text message
67
    	messageBodyPart.setText(message);
68
    	multipart.addBodyPart(messageBodyPart);
69
 
70
    	//Part 2: Attachment
71
    	messageBodyPart = new MimeBodyPart();
72
    	DataSource source = new ByteArrayDataSource(bytes, type);
73
    	messageBodyPart.setDataHandler(new DataHandler(source));
74
    	messageBodyPart.setFileName(filename);
75
    	multipart.addBodyPart(messageBodyPart);
76
 
77
    	// Put parts in message
78
    	msg.setContent(multipart);
79
    	Transport.send(msg);
80
    }
81
 
4827 mandeep.dh 82
    public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
83
        Message msg = prepareMessage(recipients, subject, from, password);
84
 
85
        //Create the multi-part object to hold the text and attachment parts
86
        Multipart multipart = new MimeMultipart();
87
 
88
        // create the message part 
89
        MimeBodyPart messageBodyPart = new MimeBodyPart();
90
        //Part 1: Text message
91
        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);
102
        }
103
 
104
        // Put parts in message
105
        msg.setContent(multipart);
106
        Transport.send(msg);
107
    }
108
 
109
    private Message prepareMessage(String[] recipients, String subject,
110
            final String from, final String password) throws AddressException,
111
            MessagingException {
2822 chandransh 112
        boolean debug = true;
113
 
114
        Properties props = new Properties();
115
        props.put("mail.smtp.host", SMTP_HOST_NAME);
116
        props.put("mail.smtp.auth", "true");
117
        props.put("mail.debug", "true");
118
        props.put("mail.smtp.port", SMTP_PORT);
119
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
120
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
121
        props.put("mail.smtp.socketFactory.fallback", "false");
122
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
123
 
124
        Session session = Session.getDefaultInstance(props,
125
                new javax.mail.Authenticator() {
126
 
127
                    protected PasswordAuthentication getPasswordAuthentication() {
128
                        return new PasswordAuthentication(from, password);
129
                    }
130
                });
131
 
132
        session.setDebug(debug);
133
 
134
        Message msg = new MimeMessage(session);
135
 
136
        //Set the from address
137
        InternetAddress addressFrom = new InternetAddress(from);
138
        msg.setFrom(addressFrom);
139
 
140
        //Set the recipients
141
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
142
        for (int i = 0; i < recipients.length; i++) {
143
            addressTo[i] = new InternetAddress(recipients[i]);
144
        }
145
        msg.setRecipients(Message.RecipientType.TO, addressTo);
146
 
147
        //Setting the Subject
148
        msg.setSubject(subject);
4827 mandeep.dh 149
        return msg;
2822 chandransh 150
    }
151
 
152
    public static void main(String args[]) throws Exception {
153
 
154
 
155
        String[] sendTo = { "chandranshu.s@shop2020.in" };
156
        String emailSubjectTxt = "A test from gmail";
157
        String emailMsgTxt = "Test Message Contents";
158
        String emailFromAddress = "chandranshu.s@shop2020.in";
159
        String password = "casa12va";
160
        String filename = "/tmp/po-10.pdf";
161
 
162
        GmailUtils utils = new GmailUtils();
163
        utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, filename);
164
        System.out.println("Sucessfully Sent mail to All Users");
165
    }
166
 
167
}