J2EE JavaMail 1.5 : Sending Email
JavaMail Sending Examples |
JavaskoolEmailer.java
package com.javaskool;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaskoolEmailer {
static Properties properties = new Properties();
static {
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.port", "465");
}
public String send(final String from,final String password, String to, String subject, String body) {
String ret = "ERROR";
try {
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
ret = "SUCCESS";
} catch (MessagingException e) {
ret = "ERROR";
e.printStackTrace();
}
return ret;
}
public static Properties getProperties() {
return properties;
}
public static void setProperties(Properties properties) {
JavaskoolEmailer.properties = properties;
}
}
TestDrive.java
package com.javaskool;
import java.io.Console;
public class TestDrive{
public static void main(String[] args) throws Exception {
JavaskoolEmailer email=new JavaskoolEmailer();
Console con=System.console();
if(con==null) return;
String recepient="smith@yahoo.com";
String subject="Testing Mail";
String Message="Hi I a james Bond";
//login Credential
String username="";
String password="";
username=con.readLine("Enter username : ");
System.out.println("Pleae Enter Password: ");
char ch[]=con.readPassword();
for(int i=0;i<ch.length;i++)
password=password+ch[i];
//System.out.println(password);
String status= email.send(username,password,recepient, subject,Message);
//String status= email.send("JAMES@gmail.com", "XXXXX", "smith@yahoo.com", "Testing Mail", "Hi I a james Bond..");
if(status.equals("SUCCESS"))
{
System.out.println("Mail has been sent Successfully.");
}
else
{
System.out.println("Mail has not been sent; FAIL");
}
}
}
Click here to download javax.mail.jar
Output
F:\Javaskool\workspace>set CLASSPATH=F:\Javaskool\workspace\lib\javax.mail.jar;.;
F:\Javaskool\workspace>javac -d . *.java
F:\Javaskool\workspace>java com.javaskool.TestDrive
Enter username : james@gmail.com
Pleae Enter Password:
Mail has been sent Successfully.
Recent Comments