JavaMail operation summary (3)

xiaoxiao2021-03-06  99

// Principle: You can see a plurality of parts. If the body is a part, the attachment is also a part, so use Bodypart to set the format of the message. ); 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");// here accessory set messageBodyPart.setDataHandler mail (new DataHandler (source)); messageBodyPart.setFileName ( "test.zip"); multipart.addBodyPart (messageBodyPart); message.setContent ( Multipart; transport.send; system.out.println ("send ok!");} catch (exception ex) {system.out.println ("fail" EX);}} // This section code 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 s Ession 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"); folder.Open (Folder.Read_write); message message [] = folder.getMess (); for (int i = 0, n = message.length; i {// get Part of the message, such as header information, sender signature, send person's email address string out_from_person = (internetdress) message [i] .GetFrom () [0]). GetPersonal (); string out_from_address = ((InternetAddress) ) Message [i] .GetFrom () [0]). getAddress ();

System.out.println ("from:" OUT_FROM_PERSON "/ T"); System.out.println ("Address:" OUT_FROM_ADDRESS "/ t"); string out_subject = message [i] .Getsubject (); system. Out.println ("Subject:" OUT_SUBJECT "/ T"); // The following code is used to get the host information of the 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 accessories 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))))))))) {// The following code saves the attachments obtained to the current directory, with part.getfilename () as file name, is both The name of the attachment.

转载请注明原文地址:https://www.9cbs.com/read-126869.html

New Post(0)