Summary of JavaMail Operations (3)

Source: Internet
Author: User
Tags auth mail readline zip
Principle: The message can be seen as a number of parts, such as the body is a part of the attachment is a part, so use BodyPart to set the format of the message
Message.addrecipient (message.recipienttype.to,new internetaddress (mail_to));
BodyPart Messagebodypart = new MimeBodyPart ();
Messagebodypart.settext ("Pardon Ideas");
Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);

Messagebodypart = new MimeBodyPart ();
DataSource Source = new Filedatasource ("./20020423/test.zip")//set of attachments for messages
Messagebodypart.setdatahandler (new DataHandler (source));
Messagebodypart.setfilename ("Test.zip");
Multipart.addbodypart (Messagebodypart);
Message.setcontent (multipart);
Transport.send (message);
System.out.println ("Send ok!");
}
catch (Exception ex)
{
System.out.println ("Faild" +ex);
}

}
This piece of code is used to send messages with attachments
void Jbutton6_actionperformed (ActionEvent e) {
Try
{
Properties props = System.getproperties ();
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");
Folder.open (Folder.read_write);
Message message[] = Folder.getmessages ();

for (int i=0,n=message.length;i {
Get part of the message, such as head information, the sender's signature, the messenger's e-mail address.
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 get body information for 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 ();
}
}
Get attachments
Multipart MP = (Multipart) message[i].getcontent ();
for (int j=0,m=mp.getcount (); j {
SYSTEM.OUT.PRINTLN ("* * *" +m+ "* * *");
Part part = Mp.getbodypart (j);
String disposition = part.getdisposition ();
if (disposition!= null) && (Disposition.equals (part. Attachment)) | | (Disposition.equals (part. INLINE)))
{

The following code saves the obtained attachment to the current directory, Part.getfilename () as the file name, and is the name of the attachment.
File filename = new file (Part.getfilename ());
for (int k=0;filename.exists (); k++)
{
filename = new File (Part.getfilename () +k);
}
FileOutputStream Myfileoutputstream = new FileOutputStream (filename);
int chunk = Part.getsize ();//the size of the attachment is not necessarily accurate.

byte [] buffer = new Byte[chunk];
InputStream instream = Part.getinputstream ();
Instream.read (Buffer,0,chunk);
Myfileoutputstream.write (Buffer,0,chunk);
Instream.close ();
Myfileoutputstream.close ();
}
}
System.out.println ("------------------------------------------------------------");
Message[i].setflag (flags.flag.deleted,true);
}
Folder.close (TRUE);
Store.close ();//Close
}
catch (Exception ej2)
{
System.out.println (EJ2);
}
}
 

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.