Summary of JavaMail operations (4)

xiaoxiao2021-03-06  99

// This code is used to send a message with information html void jButton8_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); // Setup mail body string htmlText =

Hello " " Click Me Please! "; Message .SETCONTENT (HTMLTEXT, "Text / HTML"); // text / plain Setting the format of the mail because it is with an HTML document, so use textSubject (mail_subject); // message.setText (mail_body) ; using message.setContent () time, if not with message.setText () message.setHeader (mail_head_name, mail_head_value); message.setSentDate (new Date ()); address address = new InternetAddress (mail_from, "sunxiaoming") ; message.setFrom (address); address toAddress = new InternetAddress (mail_to); message.addRecipient (Message.RecipientType.TO, toAddress); Transport.send (message); System.out.println ( "send ok!"); } Catch (exception ex) {system.out.Println ("FAILD" EX);}} // This section is used to send mail void jbutton9_actionPerformed (ActionEvent E) {Try {string file = "./20020423 / Test.gif "; Properties Props = new Properties (); Authenticator Auth = new email_autherticator (); Props.Put (" mail.smtp.host ", host); prOPs.put (" mail.smtp.auth "," "); session session = session.getDefaultInstance (props, auth); mimeMessage message = new mimeMessage (session); message.setSubject (" Embedded Image "); address address =

new InternetAddress (mail_from, "sunxiaoming"); message.setFrom (address); Address toAddress = new InternetAddress (mail_to); message.addRecipient (Message.RecipientType.TO, toAddress); message.setHeader (mail_head_name, mail_head_value); message. SetSentDate (new date ()); bodypart messagebodypart = new mimebodypart (); string htmltext = "

" ""; // joins the picture MessageBodyPart. SetContent (HtmlText, "Text / HTML"); // Principle As with the send attachment, the email looks into multiple parts, first sets each part, and then all them can match them.

// Create a related multipart to combine the parts MimeMultipart multipart = new MimeMultipart ( "related"); multipart.addBodyPart (messageBodyPart); // Create part for the image messageBodyPart = new MimeBodyPart (); // Fetch the image and associate to part DataSource fds = new FileDataSource (file); messageBodyPart.setDataHandler (new DataHandler (fds)); messageBodyPart.setHeader ( "Content-ID", "memememe"); // Add part to multi-part multipart.addBodyPart ( messageBodyPart); // Associate multipart with message message.setContent (multipart); Transport.send (message); System.out.println () "send ok!";} catch (Exception ex) {System.out.println ( "faild" ex);}} // for receiving the program information message with html void jButton7_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); store store = session.getStore "POP3"); store.connect (HO ST, 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 .Read_write; message message [] = folder.getMess (); for (int i = 0, n = message.Length; I // for (int i = 0, n = 1; 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 " / T "); System.out.println (" Address: "


New Post(0)