01: package org.dbbrowser;
02:
03: import java.security.Security;
04: import java.util.Properties;
05:
06: import javax.mail.Message;
07: import javax.mail.MessagingException;
08: import javax.mail.PasswordAuthentication;
09: import javax.mail.Session;
10: import javax.mail.Transport;
11: import javax.mail.internet.InternetAddress;
12: import javax.mail.internet.MimeMessage;
13:
14: public class TestGMail {
15: private static final String SMTP_HOST_NAME = "smtp.gmail.com";
16: private static final String SMTP_PORT = "465";
17: private static final String emailMsgTxt = "Test Message Contents";
18: private static final String emailSubjectTxt = "A test from gmail";
19: private static final String emailFromAddress = "anu.mangat@gmail.com";
20: private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
21: private static final String[] sendTo = { "anu_in_uk@hotmail.com" };
22:
23: public static void main(String args[]) throws Exception {
24: System.out.println("Sending...");
25: Security
26: .addProvider(new com.sun.net.ssl.internal.ssl.Provider());
27:
28: new TestGMail().sendSSLMessage(sendTo, emailSubjectTxt,
29: emailMsgTxt, emailFromAddress);
30: System.out.println("Sucessfully Sent mail to All Users");
31: }
32:
33: public void sendSSLMessage(String recipients[], String subject,
34: String message, String from) throws MessagingException {
35: boolean debug = true;
36:
37: Properties props = new Properties();
38: props.put("mail.smtp.host", SMTP_HOST_NAME);
39: props.put("mail.smtp.auth", "true");
40: props.put("mail.debug", "true");
41: props.put("mail.smtp.port", SMTP_PORT);
42: props.put("mail.smtp.socketFactory.port", SMTP_PORT);
43: props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
44: props.put("mail.smtp.socketFactory.fallback", "false");
45:
46: Session session = Session.getDefaultInstance(props,
47: new javax.mail.Authenticator() {
48:
49: protected PasswordAuthentication getPasswordAuthentication() {
50: return new PasswordAuthentication(
51: "dbbrowser@googlemail.com", "amkette");
52: }
53: });
54:
55: session.setDebug(debug);
56:
57: Message msg = new MimeMessage(session);
58: InternetAddress addressFrom = new InternetAddress(from);
59: msg.setFrom(addressFrom);
60:
61: InternetAddress[] addressTo = new InternetAddress[recipients.length];
62: for (int i = 0; i < recipients.length; i++) {
63: addressTo[i] = new InternetAddress(recipients[i]);
64: }
65: msg.setRecipients(Message.RecipientType.TO, addressTo);
66:
67: // Setting the Subject and Content Type
68: msg.setSubject(subject);
69: msg.setContent(message, "text/plain");
70: Transport.send(msg);
71: }
72: }
|