Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5563 amar.kumar 1
package in.shop2020.alert.util;
2
 
3
 
4
import java.io.File;
5
import java.security.Security;
6
import java.util.List;
7
import java.util.Properties;
8
 
9
import javax.activation.DataHandler;
10
import javax.activation.DataSource;
11
import javax.activation.FileDataSource;
12
import javax.mail.Message;
13
import javax.mail.MessagingException;
14
import javax.mail.Multipart;
15
import javax.mail.PasswordAuthentication;
16
import javax.mail.Session;
17
import javax.mail.Transport;
18
import javax.mail.internet.AddressException;
19
import javax.mail.internet.InternetAddress;
20
import javax.mail.internet.MimeBodyPart;
21
import javax.mail.internet.MimeMessage;
22
import javax.mail.internet.MimeMultipart;
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) throws MessagingException {
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
        // Put parts in message
46
        msg.setContent(multipart);
47
        Transport.send(msg);
48
    }
49
 
50
    public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
51
        Message msg = prepareMessage(recipients, subject, from, password);
52
 
53
        //Create the multi-part object to hold the text and attachment parts
54
        Multipart multipart = new MimeMultipart();
55
 
56
        // create the message part 
57
        MimeBodyPart messageBodyPart = new MimeBodyPart();
58
        //Part 1: Text message
59
        messageBodyPart.setText(message);
60
        multipart.addBodyPart(messageBodyPart);
61
 
62
        //Part 2: Attachment
63
        for (File file : files) {
64
            DataSource source = new FileDataSource(file);
65
            String[] names = file.getAbsolutePath().split("/");
66
            messageBodyPart = new MimeBodyPart();
67
            messageBodyPart.setDataHandler(new DataHandler(source));
68
            messageBodyPart.setFileName(names[names.length - 1]);
69
            multipart.addBodyPart(messageBodyPart);
70
        }
71
 
72
        // Put parts in message
73
        msg.setContent(multipart);
74
        Transport.send(msg);
75
    }
76
 
77
    private Message prepareMessage(String[] recipients, String subject,
78
            final String from, final String password) throws AddressException,
79
            MessagingException {
80
        boolean debug = true;
81
 
82
        Properties props = new Properties();
83
        props.put("mail.smtp.host", SMTP_HOST_NAME);
84
        props.put("mail.smtp.auth", "true");
85
        props.put("mail.debug", "true");
86
        props.put("mail.smtp.port", SMTP_PORT);
87
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
88
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
89
        props.put("mail.smtp.socketFactory.fallback", "false");
90
        //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
91
 
92
        Session session = Session.getDefaultInstance(props,
93
                new javax.mail.Authenticator() {
94
 
95
                    protected PasswordAuthentication getPasswordAuthentication() {
96
                        return new PasswordAuthentication(from, password);
97
                    }
98
                });
99
 
100
        session.setDebug(debug);
101
 
102
        Message msg = new MimeMessage(session);
103
 
104
        //Set the from address
105
        InternetAddress addressFrom = new InternetAddress(from);
106
        msg.setFrom(addressFrom);
107
 
108
        //Set the recipients
109
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
110
        for (int i = 0; i < recipients.length; i++) {
111
            addressTo[i] = new InternetAddress(recipients[i]);
112
        }
113
        msg.setRecipients(Message.RecipientType.TO, addressTo);
114
 
115
        //Setting the Subject
116
        msg.setSubject(subject);
117
        return msg;
118
    }
119
 
120
    public static void main(String args[]) throws Exception {
121
 
122
 
123
        String[] sendTo = { "build@shop2020.in" };
124
        String emailSubjectTxt = "A test from gmail";
125
        String emailMsgTxt = "Test Message Contents";
126
        String emailFromAddress = "build@shop2020.in";
127
        String password = "cafe@nes";
128
 
129
        GmailUtils utils = new GmailUtils();
130
        utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);
131
        System.out.println("Sucessfully Sent mail to All Users");
132
    }
133
 
134
}