Friday, December 5, 2008

Receiving Email Using javamail API in Eclipse

EXAMPLE FOR RECEIVING EMAIL

INTRODUCTION:

To Receive email using javamail API.We use POP3 server.

PROCEDURE:

Like sending email.

EXAMPLE:


package p1;

import java.io.*;

import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.pop3.POP3SSLStore;

import java.util.*;

public class ReceiverExam
{
private static Session session = null;
private static Store store = null;
private static String username, password;
private static Folder folder;

public static void main(String args[])throws Exception
{
try
{
username="Email ID";
password="Password";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

Properties pop3Props = new Properties();

pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
username, password);

session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
System.out.println("connected");

folder=store.getFolder("INBOX");

folder.open(Folder.READ_ONLY);

Message[] message=folder.getMessages();

for (int i = 0; i < message.length; i++)
{

System.out.println("------------ Message " + (i + 1) + " ------------");

System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");

InputStream stream = message[i].getInputStream();
while (stream.available() != 0)
{
System.out.print((char) stream.read());
}
System.out.println();
}


folder.close(true);

store.close();

}
catch(Exception e)
{
System.out.println(e);
}
}
}

Send Email using javamail API in Eclipse

JAVAMAIL

INTRODUCTION:

To enhance java technology,sun provide javamail API Which can be used to perform email functionalty. Such as sending email and receiving email and forwarding email and repling email and deleting email and sending attachment stc..

REQUIREMENT:

JavaMail API:

To perform email functionality we need javamail API. So we can download javamail API from

Download JavaMail API

Eclipse Framework:

To Download Eclipse framework from


Download Eclipse Framework


HOW TO INSTALL:

To extract downloaded javamail API file.

To Install Eclipse

PROCEDURE:

1. Open eclipse

2. File-->Project-->java-->java Project-->Give Project Name.

3. Right Click on project name and select Properties and select java Build Path on the left side. And select Add External JARS on the Right side.And Select
javamail API from where you installed.such as smtp,pop etc..

4. Write a Program Given below

5. Run java program(Right click on the javaprogram and select Run As and run.

6. Finally You Get Answer


Example For Sending Email:


package p1;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MailSender
{
final String senderEmailID = "Your Email ID For gmail";
final String senderPassword = "password";
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "465";

String receiverEmailID = null;
String emailSubject = null;
String emailBody = null;

public MailSender(String receiverEmailID, String emailSubject, String emailBody)
{
this.receiverEmailID=receiverEmailID;
this.emailSubject=emailSubject;
this.emailBody=emailBody;


Properties props = new Properties();
props.put("mail.smtp.user",senderEmailID);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(senderEmailID));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailID));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}


}
public class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(senderEmailID, senderPassword);
}
}

public static void main(String[] args)
{

MailSender mailSender=new MailSender("Receiver Email ID","Email Subjectl","Email Body");
}

}