欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

java:javamail1.6.2CreateReceiveEmailusingjdk19

时间:2023-08-01

public static void DuReceiveEmail(String pop3Host,String storeType, String user, String password){ Properties props = new Properties(); props.put("mail.pop3.host", pop3Host); props.put("mail.pop3.port", "110"); props.put("mail.pop3.starttls.enable", "false"); props.put("mail.store.protocol", "pop3"); Session session = Session.getInstance(props); try { Store mailStore = session.getStore(storeType); mailStore.connect(pop3Host, user, password); Folder folder = mailStore.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message[] emailMessages = folder.getMessages(); System.out.println("Total Message - " + emailMessages.length); //Iterate the messages for (int i = 0; i < emailMessages.length; i++) { Message message = emailMessages[i]; Address[] toAddress = message.getRecipients(Message.RecipientType.TO); System.out.println(); System.out.println("Email " + (i+1) + "-"); System.out.println("Subject - " + message.getSubject()); System.out.println("From - " + message.getFrom()[0]); System.out.println("To - "); for(int j = 0; j < toAddress.length; j++){ System.out.println(toAddress[j].toString()); } System.out.println("Text - " + message.getContent().toString()); } folder.close(false); mailStore.close(); } catch (Exception e) { e.printStackTrace(); System.err.println("Error in receiving email."); } } public static void receiveEmail(String pop3Host, String storeType, String user, String password) { try { //1) get the session object Properties properties = new Properties(); properties.put("mail.pop3.host", pop3Host); Session emailSession = Session.getDefaultInstance(properties); //2) create the POP3 store object and connect with the pop server POP3Store emailStore = (POP3Store) emailSession.getStore(storeType); emailStore.connect(user, password); //3) create the folder object and open it Folder emailFolder = emailStore.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); //获取邮箱基本信息 // Map map = DuMailHelper.emailInfo(emailFolder); // System.out.println(map); //4) retrieve the messages from the folder in an array and print it Message[] messages = emailFolder.getMessages(); // parseMessage(messages); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; System.out.println("---------------------------------"); System.out.println("Email Number " + (i + 1)); System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); System.out.println("Text: " + message.getContent().toString()); } //5) close the store and folder objects emailFolder.close(false); emailStore.close(); } catch (NoSuchProviderException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。