java: javamail 1.6.2 Create Receive Email using jdk 19
尝试获取编码(pop3message.getEncoding()),则会获得此contentType(UTF-8一个)
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 | /** * 接收邮件 * 中文是乱码 未解决 * @param pop3Host pop.163.com * @param storeType pop3 * @param user geovindu@163.com * @param password geovindu * @author geovindu * @serialData@ * * * **/ 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." ); } } /** * 接收邮件 * 中文是乱码 未解决 * @param pop3Host pop.163.com * @param storeType pop3 imap * @param user geovindu@163.com * @param password * @author geovindu * * * **/ 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<String, Integer> 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();} } |
https://javaee.github.io/javamail/FAQ#commonmistakes
https://www.geeksforgeeks.org/sending-email-java-ssltls-authentication/
https://stackoverflow.com/questions/13980922/java-mail-api-encoding-problems
https://www.go4expert.com/articles/send-receive-mails-java-javamail-t3649/
https://community.oracle.com/tech/developers/discussion/1590874/send-an-email-encoded-in-utf-8-with-javamail-character-problems
https://javaee.github.io/javamail/docs/api/
QQ邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:
类型 |
服务器名称 |
服务器地址 |
非SSL协议端口号 |
SSL协议端口号 |
发件服务器 |
SMTP |
smtp.qq.com |
25 |
465/587 |
收件服务器 |
POP |
pop.qq.com |
110 |
995 |
收件服务器 |
IMAP |
imap.qq.com |
143 |
993
|
163邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:
类型 | 服务器名称 | 服务器地址 | SSL协议端口号 | 非SSL协议端口号 |
收件服务器 | POP | pop.163.com | 995 | 110 |
收件服务器 | IMAP | imap.163.com | 993 | 143 |
发件服务器 | SMTP | smtp.163.com | 465/994 | 25 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2018-02-02 jQuery:SP.NET Autocomplete Textbox Using jQuery, JSON and AJAX