Friday, August 26, 2011

javamail: to access mail through java

The JavaMail API is a set of abstract APIs that model a mail system.The JavaMail API provides facilities for reading and sending email. Service providers implement particular protocols. Several service providers are included with the JavaMail API package; others are available separately.


Download Javamail



If a web-based email services provide POP3 or IMAP access, JavaMail can probably be used to access it.


IMAP:IMAP stands for Internet Message Access Protocol. It is a method of accessing electronic mail messages stored on a (possibly shared) mail server. In other words, it permits a "client" email program to access remote message stores as if they were local.


SMTP
SMTP stands for Simple Mail Transfer Protocol. It is used to transfer RFC822-style messages between different mail hosts as well as to submit new messages to a host for delivery. SMTP is in very wide use (it originated in 1982).



MIME
MIME and RFC822 are the standards for describing email messages that are sent across the Internet. The javax.mail.internet subpackage (which is part of the JavaMail APIs) provides a complete implementation of these two packages. MIME is specified by the following RFCs: RFC2045, RFC2046, RFC2047.




POP3

POP3 stands for Post Office Protocol version 3. POP3 is a very limited protocol for accessing a single mailbox. It is much less capable than IMAP. POP3 is very widely used and is defined by RFC1939.


To use javamail with your project, Unzip the distribution zip file and edit your CLASSPATH environment variable to include the mail.jar file that was included with the JavaMail API distribution.

At contect of Netbeans, javamail can be addred asfollowing steps:
Q: How do I add the JavaMail library to my NetBeans Java Project?
A: Use the Library Manager to create a library and add a reference to that library to your project.

1. Inside Netbeans, open the Library Manager (Tools > Libraries from the main menu bar).
2. Select
3. Enter a library name, e.g., "JavaMail", and hit . Do not check server library.
4. Make sure the new library is selected under "Libraries" on the left.
5. Select the "Classpath" tab on the right and choose
6. Using the file browser, select mail.jar from your JavaMail installation (e.g. .../javamail-1.4.3/mail.jar), and hit to accept.
7. Select the "Javadoc" tab and choose
8. Using the file browser, select the folder where the JavaMail javadoc index files are (e.g. .../javamail-1.4.3/docs/javadocs"), and hit to accept.
9. If you downloaded the JavaMail source code, you can set the source code reference on the "Sources" tab. This can be helpful for debugging. Using the file browser, select the source root for the JavaMail sources (".../javamail-1.4.3/mail/src/main/java" for 1.4.3) and hit to accept.

Note: If you are using JDK 1.5.0 or earlier you will need to add the JavaBeans Activation Framework library as well. You can create a separate library in a manner similar to the above, or just add activation.jar (and javadoc/source references if desired) to this library definition.

Now add a reference to this library to your project.

1. Open your project in NetBeans and make sure the "Projects" tab is visible.
2. Right click your project in the project explorer and select "Properties"
3. In the Properties dialog, select "Libraries" in the tree on the left and make sure the "Compile" tab is selected.
4. Click
5. Locate and select the library you created above and click "Add Library". It should be added to the list of compile-time libraries.
6. Click "OK" and you're done.



The JavaMail API package does not include any mail servers. To use the JavaMail API package, you'll need to have access to an IMAP or POP3 mail server (for reading mail) and/or an SMTP mail server (for sending mail). These mail servers are usually provided by your Internet Service Provider or are a part of your organization's networking infrastructure.



JavaMail 1.4 is capable of sending and reading messages using Gmail. All that's required is to properly configure JavaMail. I'll illustrate the proper configuration using the demo programs that come with JavaMail - msgshow.java and smtpsend.java.

Let's assume your Gmail username is "user@gmail.com" and your password is "passwd".

To read mail from your Gmail Inbox, invoke msgshow as follows:

java msgshow -D -T pop3s -H pop.gmail.com -U user -P passwd


By reading the msgshow.java source code, you can see how these command line arguments are used in the JavaMail API. You should first try using msgshow as shown above, and once that's working move on to writing and configuring your own program to use Gmail. The following code fragment shows a simple way to incorporate the needed configuration in your application:

String host = "pop.gmail.com;
String username = "user";
String password = "passwd";
// ...
Store store = session.getStore("pop3s");
store.connect(host, username, password);



To connect to Gmail using the IMAP protocol instead of the POP3 protocol, simply change the host name "pop.gmail.com" to "imap.gmail.com" and change the protocol name "pop3s" to "imaps" in the above instructions.

To send a message through Gmail, invoke smtpsend as follows:

java -Dmail.smtps.host=smtp.gmail.com -Dmail.smtps.auth=true
smtpsend -d -S -M smtp.gmail.com -U user -P passwd -A user@gmail.com


(Note that I split the command over two lines for display, but you should type it on one line.)

A bug in older versions of the smtpsend command causes it to set the incorrect properties when using the -S (SSL) option, so we work around that bug by setting them on the command line. The smtpsend program uses the System properties when creating the JavaMail Session, so the properties set on the command line will be available to the JavaMail Session.

The smtpsend program will prompt for a subject and message body text. End the message body with ^D on UNIX or ^Z on Windows.

Again, you can read the smtpsend.java source code to see how the command line arguments are used in the JavaMail API. The following code fragment shows a simple way to incorporate the needed configuration in your application:


    String host = "smtp.gmail.com;

String username = "user";
String password = "passwd";
Properties props = new Properties();
props.put("mail.smtps.auth", "true");
// ...
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport t = session.getTransport("smtps");
try {
t.connect(host, username, password);
t.sendMessage(msg, msg.getAllRecipients());
} finally {
t.close();
}









No comments: