Simple example of a letter of receipt: Simplereceiver

Source: Internet
Author: User
Tags command line mail connect readline
Import javax.mail.*;
Import javax.mail.internet.*;
Import java.util.*;
Import java.io.*;

/**
* A Simple email receiver class.
*/
public class simplereceiver{
/**
* Main method to receive messages from the mail server specified
* As command line arguments.
*/
public static void Main (String args[]) {
try{
String Popserver=args[0];
String Popuser=args[1];
String poppassword=args[2];
Receive (Popserver, Popuser, Poppassword);
}catch (Exception ex) {
System.out.println ("Usage:java com.lotontech.mail.SimpleReceiver" + "Popserver popuser Poppassword");
}
System.exit (0);
}
/**
* "Receive" method to fetch messages and process them.
*/
public static void Receive (String popserver, String popuser, String Poppassword) {
Store Store=null;
Folder Folder=null;
try{
--Get hold of the ' default session--
Properties props = System.getproperties ();
Session session = Session.getdefaultinstance (props, null);
--Get hold's a POP3 message store, and connect to it--
store = Session.getstore ("POP3");
Store.connect (Popserver, Popuser, Poppassword);
--Try to get hold of the default folder--
folder = Store.getdefaultfolder ();
if (folder = = null) throw new Exception ("No default folder");
--... and its INBOX--
folder = Folder.getfolder ("INBOX");
if (folder = = null) throw new Exception ("No POP3 INBOX");
--Open the folder for read only--
Folder.open (folder.read_only);
--Get the message wrappers and process them--
message[] msgs = Folder.getmessages ();
for (int msgnum = 0; msgnum < msgs.length; msgnum++) {
Printmessage (Msgs[msgnum]);
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
finally{
--Close down nicely--
try{
if (Folder!=null) Folder.close (false);
if (store!=null) store.close ();
}catch (Exception ex2) {
Ex2.printstacktrace ();
}
}
}
/**
* "Printmessage ()" method to print a message.
*/
public static void Printmessage (Message message) {
try{
Get the header information
String from= ((internetaddress) message.getfrom () [0]). getpersonal ();
if (from==null) from= (internetaddress) message.getfrom () [0]). getaddress ();
System.out.println ("From:" +from);
String Subject=message.getsubject ();
System.out.println ("SUBJECT:" +subject);
---Get the message part (i.e. the itself)--
Part messagepart=message;
Object content=messagepart.getcontent ();
--or its-a-multipart message-
if (content instanceof Multipart) {
Messagepart= ((Multipart) content). Getbodypart (0);
System.out.println ("[Multipart message]");
}
--Get the content type--
String Contenttype=messagepart.getcontenttype ();
--If The content is plain text, we can print it--
System.out.println ("CONTENT:" +contenttype);
if (Contenttype.startswith ("Text/plain") | |
Contenttype.startswith ("text/html")) {
InputStream is = Messagepart.getinputstream ();
BufferedReader reader=new BufferedReader (New InputStreamReader (IS));
String Thisline=reader.readline ();
while (Thisline!=null) {
System.out.println (Thisline);
Thisline=reader.readline ();
}
}
System.out.println ("-----------------------------");
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.