Interview Questions On Java,Java EE


Enter your email address:



Add to Technorati Favorites
Google PageRank Checker
Showing posts with label Java Mail. Show all posts
Showing posts with label Java Mail. Show all posts

Wednesday, March 19, 2008

Sending EMail To GMail SMTP Server Using Java Mail APIs


import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* This application will require Java Mail API and which can be downloaded from
* http://java.sun.com/products/javamail/downloads/index.html JavaBeansTM
* Activation Framework extension or JAF (javax.activation) can be downloaded
* from
* http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html.
*
* @author pundeerd
*
*
*/
public class MailingApplication {

/**
* @param args
*/
public static void main(String[] args) {

// recepients
String[] to = { "abc@gmail.com" };
String subject = "Test Email";
String message = "Hello there";
// sender
String from = "xyz@gmail.com";
try {
postMail(to, subject, message, from);
} catch (MessagingException e) {

e.printStackTrace();
}

}

private static void postMail(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = false;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Set the host smtp address
Properties properties = new Properties();
properties.put("mail.smtp.user", "xyz@gmail.com");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtps.auth", "true ");
properties.put("mail.smtp.debug", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");

// create some properties and get the default Session

Session session = Session.getDefaultInstance(properties, null);

session.setDebug(true);
session.setDebug(debug);

// create a message
MimeMessage msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < tr =" session.getTransport(">
Continue reading...
 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker