最近发现一个问题,小伙伴给我发的邮件,收和回都不及时。于是我现在将所有的邮箱,通过POP3/SMTP协议整合到了一起。再配合小米手环,就能做到邮件无遗漏。
一、邮箱常用协议
邮箱中常用三类协议
- POP3
- Post Office Protocol version 3
- 用于获取邮件。适用于离线访问,客户端从服务端同步了一份数据。实际是两份数据。
- 适合离线访问
- IMAP
- Internet Message Access Protocol
- 用于获取邮件。适用于在线访问,客户端直接访问的服务端数据。实际是同一份数据。
- 适合多设备之间同步数据,比如邮件删除会所有设备都能同步。
- SMTP
- Simple Mail Transfer Protocol
- 用于接收邮件
因此,想要实现一套邮箱服务,就要有收和发,那么就有两种组合方式
二、阿里邮箱
2.1 配合域名使用阿里邮箱
此处针对阿里邮箱作为示例,简单记录一下。
2.2 代码示例
Java Maven 引入邮件依赖
1 2 3 4 5
| <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
|
pop3
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
| import javax.mail.*; import java.util.Properties;
public class POP3Example {
public static void list(String email, String emailPassword) { Properties properties = new Properties(); properties.setProperty("mail.store.protocol", "pop3"); properties.setProperty("mail.pop3.host", "pop3.meethigher.top"); properties.setProperty("mail.pop3.port", "110"); properties.setProperty("mail.pop3.ssl.enable", "false"); properties.setProperty("mail.debug", "true"); Session session = Session.getInstance(properties); try (Store store = session.getStore("pop3")) { store.connect(email, emailPassword); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); for (Message message : messages) { String sender = message.getFrom()[0].toString(); String subject = message.getSubject(); String content = message.getContent().toString();
System.out.println("Sender: " + sender); System.out.println("Subject: " + subject); System.out.println("Content: " + content); System.out.println("------------------------------------"); } } catch (Exception e) { e.printStackTrace(); System.out.println("获取邮件失败"); } } }
|
imap
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
| import javax.mail.Folder; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; import java.util.Properties;
public class IMAPExample {
public static void list(String email, String emailPassword) { Properties properties = new Properties(); properties.setProperty("mail.store.protocol", "imap"); properties.setProperty("mail.imap.host", "imap.meethigher.top"); properties.setProperty("mail.imap.port", "143"); properties.setProperty("mail.imap.ssl.enable", "false"); properties.setProperty("mail.debug", "true"); Session session = Session.getInstance(properties); try (Store store = session.getStore("imap")) { store.connect(email, emailPassword); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); for (Message message : messages) { String sender = message.getFrom()[0].toString(); String subject = message.getSubject(); String content = message.getContent().toString();
System.out.println("Sender: " + sender); System.out.println("Subject: " + subject); System.out.println("Content: " + content); System.out.println("------------------------------------"); } } catch (Exception e) { e.printStackTrace(); System.out.println("获取邮件失败"); } } }
|
smtp
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
| import javax.mail.*; import javax.mail.internet.*; import java.util.Properties;
public class SMTPExample {
public static void send(String email, String emailPassword, String targetEmail) { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.meethigher.top"); properties.setProperty("mail.smtp.port", "80"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.ssl.enable", "false"); properties.setProperty("mail.debug", "true"); Session session = Session.getInstance(properties); try (Transport transport = session.getTransport()) { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(email)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(targetEmail)); message.setSubject("JavaMail SMTP Example"); message.setText("This is a test email sent from JavaMail API."); transport.connect(email, emailPassword); transport.sendMessage(message, message.getAllRecipients()); System.out.println("邮件发送成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("邮件发送失败"); } } }
|
三、致谢参考
阿里邮箱IMAP、POP、SMTP地址和端口信息
阿里云(万网)域名使用阿里邮箱如何设置解析?