Summary of JavaMail operations (2)

Source: Internet
Author: User
Tags auth
// This code is used to authenticate users on the server.
Public class Email_Autherticator extends Authenticator
{
Public Email_Autherticator ()
{
Super ();
}
Public PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication (username, password );
}
}
// This program is used to receive emails
Void jbutton2_actionreceivmed (ActionEvent e ){
Try
{
Properties props = System. getProperties (); // Obtain System variables
Authenticator auth = new Email_Autherticator ();
Props. put ("mail. smtp. host", host );
Props. put ("mail. smtp. auth", "true ");
Session session = Session. getDefaultInstance (props, auth); // Create a session
Store store = session. getStore ("pop3 ");
Store. connect (host, username, password );
// After connecting to the Store, you can get a Folder, which must be opened before you can read messages from it:
Folder folder = store. getFolder ("INBOX"); // Obtain a Folder after you connect to the Store. The default value is INDEX.
Folder. open (Folder. READ_WRITE); // READ_ONLY indicates the open mode.
Message message [] = folder. getMessages (); // Obtain mail information from the folder
// You can obtain the email information in two ways. getContent () is used to obtain the subject information of the email. WriteTo () can be used to obtain all the mail information, including the header information.
// System. out. println (MimeMessage) message). getContent ());
For (int I = 0, n = message. length; I {
String out_from_person = (InternetAddress) message [I]. getFrom () [0]). getPersonal ();
String out_from_address = (InternetAddress) message [I]. getFrom () [0]). getAddress ();
System. out. println ("From:" + out_from_person + "");
System. out. println ("Address:" + out_from_address + "");
String out_subject = message [I]. getSubject ();
System. out. println ("Subject:" + out_subject + "");
// Use the following code to obtain the body information of an email:
Part messagePart = message [I];
Object out_content = messagePart. getContent ();
If (out_content instanceof Multipart)

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.