J2EE JavaMail 1.5
JavaMail Concept? |
- It is solution for eMail
- The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications.
- The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.
Latest Release
JavaMail API 1.5.1
Session API
public final class Session extends Object
The Session class represents a mail session and is not subclassed. It collects together properties and defaults used by the mail API’s. A single default session can be shared by multiple applications on the desktop. Unshared sessions can also be created.
Session has many static methods like one below
public static Session getDefaultInstance(Properties props, Authenticator authenticator) : Get the default Session object.
Transport
public abstract class Transport extends Service – An abstract class that models a message transport. Subclasses provide actual implementations.
Transport extends the Service class, which provides many common methods for naming transports, connecting to transports, and listening to connection events.
Transport has many static methods like
static void send(Message msg) : Send a message.
static void send(Message msg, Address[] addresses) : Send the message to the specified addresses, ignoring any recipients specified in the message itself.
Message
public abstract class Message extends Object implements Part
This class models an email message. This is an abstract class. Subclasses provide actual implementations.
Message implements the Part interface. Message contains a set of attributes and a “content”. Messages within a folder also have a set of flags that describe its state within the folder.
Message defines some new attributes in addition to those defined in the Part interface. These attributes specify meta-data for the message – i.e., addressing and descriptive information about the message.
Message objects are obtained either from a Folder or by constructing a new Message object of the appropriate subclass. Messages that have been received are normally retrieved from a folder named “INBOX”.
Direct Known Subclasses is MimeMessage
Message has many methods like
Methods | Description |
---|---|
abstract void setFrom(Address address) | Set the “From” attribute in this Message. |
void setRecipient(Message.RecipientType type, Address address) | Set the recipient address. |
abstract void setRecipients(Message.RecipientType type, Address[] addresses) | Set the recipient addresses. |
void setReplyTo(Address[] addresses) | Set the addresses to which replies should be directed. |
abstract void setSentDate(Date date) | Set the sent date of this message. |
abstract void setSubject(String subject) | Set the subject of this message. |
void setFlag(Flags.Flag flag, boolean set) | Set the specified flag on this message to the specified value. |
abstract void setFlags(Flags flag, boolean set) | Set the specified flags on this message to the specified value. |
InternetAddress
public class InternetAddress extends Address implements Cloneable
This class represents an Internet email address using the syntax of RFC822. Typical address syntax is of the form “user@host.domain” or “Personal Name <user@host.domain>”.
MessagingException
public class MessagingException extends Exception
The base class for all exceptions thrown by the Messaging classes
JavaMail 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 {
//Get the default Session object.
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