JavaMail Mail Receive small example

Source: Internet
Author: User

Package Demo2;


Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import java.util.Properties;


Import Javax.mail.Folder;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Store;


public class JavaMailDemo1 {



public static void Main (string[] args) throws Exception {

String pop3server = "pop3.sohu.com";
String protocol = "POP3";
String user = "Z_p_u";
String pwd = "**********";

Create a Properties object with specific connection information
Properties Props = new properties ();
Props.setproperty ("Mail.store.protocol", protocol);
Props.setproperty ("Mail.pop3.host", pop3server);

Using the Properties object to get the Session object
Session session = Session.getinstance (props);
Session.setdebug (TRUE);

Use Session object to get store object and connect POP3 server
Store store = Session.getstore ();
Store.connect (pop3server, user, PWD);

Get the Mail folder folder object in the mailbox, open as Read only
Folder folder = Store.getfolder ("Inbox");
Folder.open (folder.read_only);

Get the Mail folder all messages within a folder message object
Message [] messages = Folder.getmessages ();

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

String subject = Messages[i].getsubject ();
String from = (Messages[i].getfrom () [0]). toString ();

System.out.println ("First" + (i+1) + "Subject of mail:" + subject);
System.out.println ("First" + (i+1) + "Sender address of the message:" + from);

System.out.println ("Do you want to open the message (yes/no)?:");
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
String input = Br.readline ();
if ("Yes". Equalsignorecase (Input)) {
Direct output to console
Messages[i].writeto (System.out);
}
}
Folder.close (FALSE);
Store.close ();



}

}



Package Demo2;


Import java.util.Properties;


Import Javax.mail.Folder;
Import Javax.mail.Message;
Import Javax.mail.Multipart;
Import javax.mail.Session;
Import Javax.mail.Store;


public class JavaMailDemo2 {


public static void Main (string[] args) throws exception{



Properties Props=new properties ();
Props.setproperty ("Mail.store.protocol", "POP3");
Props.setproperty ("Mail.pop3.host", "pop3.sohu.com");


Session session=session.getinstance (props);
Session.setdebug (TRUE);

Store Store=session.getstore ();
Store.connect ("pop3.sohu.com", "Z_p_u", "***********");

Folder folder=store.getfolder ("Inbox");
Folder.open (folder.read_only);
System.out.println (Folder.getmessagecount () + "-----------");


Message msg[]=folder.getmessages ();

for (Message msgg:msg)
{

Multipart mp= (Multipart) msgg.getcontent ();

for (int i=0;i<mp.getcount (); i++)
{
System.out.println (Mp.getbodypart (i). GetContent ());

System.out.println (Mp.getbodypart (i). getContentType ());


}
}



}


}



















JavaMail Mail Receive small example

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.