Summary of JavaMail operations (2)

Source: Internet
Author: User
Tags auth mail connect variables readline
This code is used to authenticate the user to the server
public class Email_autherticator extends authenticator
{
Public Email_autherticator ()
{
Super ();
}
Public Passwordauthentication getpasswordauthentication ()
{
return new Passwordauthentication (Username,password);
}
}
This program is for receiving mail
void Jbutton2_actionperformed (ActionEvent e) {
Try
{
Properties props = System.getproperties (); Get 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); Establish session
Store store = Session.getstore ("POP3");
Store.connect (Host,username,password);

After connecting to the store,you can get a Folder,which must is opened before you can read messages from it:
Folder folder = Store.getfolder ("INBOX");//After connecting to the store, get a directory, the default is index
Folder.open (folder.read_write);//read_only for open mode
Message message[] = Folder.getmessages ()//Get mail information from folder

There are two ways to get message information, getcontent () to get the message's body information. and WriteTo () can be used to get all the information about the message, 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+ ")";

The following code is used to obtain the body information of a message
Part messagepart = Message[i];
Object out_content = Messagepart.getcontent ();
if (out_content instanceof Multipart)
{
MessagePart = ((Multipart) out_content). Getbodypart (0);
System.out.println ("[Multipart message]");
}
String Out_content_type = Messagepart.getcontenttype ();
System.out.println ("CONTENT:" +out_content_type);

if (Out_content_type.startswith ("Text/plain") | | | out_content_type.startswith ("text/html")
{
InputStream ipstm = Messagepart.getinputstream ();
BufferedReader bufreader = new BufferedReader (new InputStreamReader (ipstm));
String thisline = Bufreader.readline ();
while (Thisline!= null)
{
System.out.println ("Thisline:" +thisline);
Thisline = Bufreader.readline ();
}
}
System.out.println ("------------------------------------------------------------");
Message[i].setflag (flags.flag.deleted,true)//Last Delete server-side mail
}
Deleted,answered,draft,flagged,recent,seen,user
Folder.close (true);//true Delete messages that have been marked as delete, and if False, do not delete
Store.close ();//Close
}
catch (Exception ej2)
{
System.out.println (EJ2);
}
}

void Jbutton4_actionperformed (ActionEvent e) {
Try
{//This program is for reply mail
Properties props = System.getproperties (); Get System Variables
Authenticator auth = new Email_autherticator (); Get Ufffd Jin?
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth); Establish session
Store store = Session.getstore ("POP3");
Store.connect (Host,username,password);

Folder folder = Store.getfolder ("INBOX");
Folder.open (Folder.read_write);
Message message[] = Folder.getmessages ();

for (int i=0,n=message.length;i {
String Out_from_person = ((internetaddress) message[i].getfrom () [0]). Getpersonal ()//Get the signature of the sender of the message

String out_from_address = ((internetaddress) message[i].getfrom () [0]). getaddress ();
System.out.println (out_from_address);

Message forward = new MimeMessage (session);
Forward.setsubject ("FWD:" +message[i].getsubject ());
Forward.setfrom (New InternetAddress (mail_to));
Forward.addrecipient (message.recipienttype.to,new internetaddress (out_from_address));

BodyPart Messagebodypart = new MimeBodyPart ();
Messagebodypart.settext ("Here's the original message:");

Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);

Messagebodypart = new MimeBodyPart ();
Messagebodypart.setdatahandler (Message[i].getdatahandler ());

Multipart.addbodypart (Messagebodypart);
Forward.setcontent (multipart);

Transport.send (forward);
Message[i].setflag (flags.flag.deleted,true);//deleted,answered,draft,flagged,recent,seen,user
}
Folder.close (TRUE);
Store.close ();//Close
}
catch (Exception ej2)
{
System.out.println (EJ2);
}

}

void Jbutton5_actionperformed (ActionEvent e) {
Try
{
Properties Props = new properties ();
Authenticator auth = new Email_autherticator ();

Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth);
MimeMessage message = new MimeMessage (session);
Message.setcontent ("Hello", "Text/plain");
Message.setsubject (Mail_subject);
Message.settext (Mail_body);
Message.setheader (Mail_head_name,mail_head_value);
Message.setsentdate (New Date ());
Message.setfrom (New InternetAddress (Mail_from, "sunxiaoming")); To Signal Mans

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.