Summary of JavaMail operations (2)

xiaoxiao2021-03-06  98

// This code is used for user authentication server public class Email_Autherticator extends Authenticator {public Email_Autherticator () {super ();} public PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (username, password);}} // the program to receive a message 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); // Established session store store = session.getStore ("POP3"); store.connect (Host, Username, password); // after connection to the store, you can get a folder, Which Must Be Opened Before You Can Read Messages from It: Folder Folder = Store.getFolder ("Inbox"); // After connecting to the Store, Take a folder, generally the default is index folder.open (Folder.Read_WRITE); // read_only is open mode message message [] = folder.getMess (); // Get mail information from folder // can be used Way to get mail information, getContent () is used to get the main information of the message.

Writeto () can be used to obtain all information of the message, including header information // system.out.println ((mimeMessage) .getContent ()); for (int i = 0, n = message.length; i {string out_from_person = ("(Internetdress) message [i] .GetFrom () [0]). getPersonal (); string out_from_address = (InternetDress) 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 text message 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 bufreade r = 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); // finally delete the server side mail} // deleted , Answered, Draft, Flagged, Recent, Seen, User Folder.close (TRUE) } catch (Exception EJ2) {system.out.println (EJ2);

}}} void jbutton4_Actionperformed (ActionEvent E) {Try {// This program is a reply message Properties PROPS = system.getProperties (); // Get system variable authenticator auth = new email_autherticator (); // get? / uffd 衿 Charm? Props.PUT ("mail.smtp.host", host; prOPs.put ("mail.smtp.auth", "true"); session session = session.getDefaultInstance (props, auth); // Establish a session store store = Session.getStore; 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 mail senders signature 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 InternetdRess (Mail_to)); Forward.AddRecipient (Message.RecipientType.to, New Internet_Address); BodyPart messageBodyPart = new MimeBodyPart (); messageBodyPart.setText ( "Here you go with the original message: / n / n"); 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, seen, usr} folder.close (TRUE); store.close (); // off} catch (Exception EJ2) {system.out.println (EJ2);

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

New Post(0)