Subversion Repositories SmartDukaan

Rev

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