1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| package com.cmiot.onepark.email;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service;
import javax.mail.*; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.search.FlagTerm; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Properties;
@Service public class Receiver {
@Autowired private GmailProperties gmailProperties;
@Scheduled(fixedRate = 60000) public void receiveEmails() throws NoSuchProviderException, InterruptedException {
try { URL url = new URL("https://api.ipify.org?format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } System.out.println("Response: " + content.toString()); } } catch (Exception e) { e.printStackTrace(); }
System.out.println("-------------运行监听任务---------------------------------------------");
Properties props = new Properties(); props.put("mail.imap.ssl.enable", "true"); props.put("mail.imap.host", gmailProperties.getHost()); props.put("mail.imap.port", gmailProperties.getPort()); props.put("mail.imap.connectiontimeout", "300000"); props.put("mail.imap.timeout", "300000"); props.put("mail.imap.writetimeout", "300000");
props.put("mail.smtp.auth", "true"); String user = gmailProperties.getUsername().split("@")[0]; props.put("mail.imap.user", user); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { System.out.println("PasswordAuthentication user: " + gmailProperties.getUsername()); System.out.println("PasswordAuthentication password: " + gmailProperties.getPassword()); return new PasswordAuthentication(gmailProperties.getUsername(), gmailProperties.getPassword()); } });
session.setDebug(true); try (Store store = session.getStore("imap")) { System.out.println("Full URL: " + store.getURLName().toString()); System.out.println("Host: " + store.getURLName().getHost()); System.out.println("Username: " + store.getURLName().getUsername());
store.connect("imap.gmail.com", gmailProperties.getUsername(), gmailProperties.getPassword()); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); inbox.open(Folder.READ_WRITE); FlagTerm unseenFlagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Message[] messages = inbox.search(unseenFlagTerm); System.out.println("收件箱的邮件数:" + messages.length);
for (Message message : messages) { if (message instanceof MimeMessage) { MimeMessage mimeMessage = (MimeMessage) message; System.out.println("----------------------------------------------------------"); System.out.println("Subject: " + mimeMessage.getSubject()); System.out.println("From ToString: " + mimeMessage.getFrom()[0].toString()); System.out.println("From: " + mimeMessage.getFrom()[0]); System.out.println("Content: " + getTextFromMessage(mimeMessage)); System.out.println("----------------------------------------------------------");
message.setFlag(Flags.Flag.SEEN, true); } } } catch (MessagingException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); }
}
private static String getTextFromMessage(Message message) throws Exception { String result = ""; if (message.isMimeType("text/plain")) { result = message.getContent().toString(); } else if (message.isMimeType("multipart/*")) { MimeMultipart mimeMultipart = (MimeMultipart) message.getContent(); result = getTextFromMimeMultipart(mimeMultipart); } return result; }
private static String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws Exception { StringBuilder result = new StringBuilder(); int count = mimeMultipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = mimeMultipart.getBodyPart(i); if (bodyPart.isMimeType("text/plain")) { result.append(bodyPart.getContent()); } else if (bodyPart.isMimeType("text/html")) { String html = (String) bodyPart.getContent(); result.append(org.jsoup.Jsoup.parse(html).text()); } else if (bodyPart.getContent() instanceof MimeMultipart) { result.append(getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent())); } } return result.toString(); } }
|