当前位置:   article > 正文

java mail发送、接收邮件_java 接收邮件

java 接收邮件

java mail接收邮件

1、引入java mail依赖

<dependency>
    <groupId>org.eclipse.angus</groupId>
    <artifactId>angus-mail</artifactId>
    <version>2.0.2</version>
</dependency>

2、编写代码

注意:下述代码中的服务器地址,用户名均是博主自己搭建的邮件服务器中的信息,请对应修改为自己的信息,邮件服务器搭建可以参考 

Apache James邮件服务器搭建(windows)_Steven-Russell的博客-CSDN博客Apache James邮件服务器搭建(linux)_Steven-Russell的博客-CSDN博客

  1. package com.wd;
  2. import jakarta.mail.*;
  3. import jakarta.mail.search.FlagTerm;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Properties;
  8. public class ReceiverPop {
  9. public static void main(String[] args) throws Exception {
  10. Properties props = new Properties();
  11. props.put("mail.pop3.host", "192.168.133.135");
  12. props.put("mail.pop3.port", 110);
  13. Session session = Session.getInstance(props, new Authenticator() {
  14. @Override
  15. protected PasswordAuthentication getPasswordAuthentication() {
  16. return new PasswordAuthentication("russell@st.com", "123456");
  17. }
  18. });
  19. Store store = session.getStore("pop3");
  20. store.connect();
  21. Folder folder = store.getFolder("INBOX");
  22. folder.open(Folder.READ_WRITE);
  23. FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); //false代表未读,true代表已读
  24. Message[] messages = folder.search(ft);
  25. Message lastMessage = messages[messages.length - 1];
  26. System.out.println(new FormatMess(lastMessage));
  27. }
  28. private static class FormatMess {
  29. private final String subject;
  30. private final String content;
  31. private final String from;
  32. private final Date sendDate;
  33. public FormatMess(Message message) throws Exception {
  34. this.subject = message.getSubject();
  35. this.content = String.valueOf(message.getContent());
  36. this.sendDate = message.getSentDate();
  37. Address[] messageFrom = message.getFrom();
  38. List<String> messageFromList = new ArrayList<>();
  39. for (Address address : messageFrom) {
  40. messageFromList.add(address.toString());
  41. }
  42. this.from = String.join(",", messageFromList);
  43. }
  44. @Override
  45. public String toString() {
  46. return "=======start========" + System.lineSeparator()
  47. + "sendDate=" + sendDate + System.lineSeparator()
  48. + "from=" + from + System.lineSeparator()
  49. + "subject=" + subject + System.lineSeparator()
  50. + "content=" + content + System.lineSeparator()
  51. + "=======end========";
  52. }
  53. }
  54. }

3、发送邮件,可以参考如下链接使用curl命令进行Apache James邮件服务器搭建(linux)_Steven-Russell的博客-CSDN博客

curl -ikv --url 'smtps://192.168.133.135:465' --mail-from 'steven@st.com' --mail-rcpt 'russell@st.com' --upload-file /root/email.txt --user 'steven@st.com:123456'

编辑文件/root/email.txt

From: steven <steven@st.com>
To: russell <russell@st.com>
Subject: st.com test email

// 注意空行

test email info.

4、接收邮件,可以发现已经接收到了邮件信息

java-mail发送邮件:

1、引入java mail依赖

<dependency>
    <groupId>org.eclipse.angus</groupId>
    <artifactId>angus-mail</artifactId>
    <version>2.0.2</version>
</dependency>

2、编写代码

  1. package com.wd;
  2. import jakarta.mail.Message;
  3. import jakarta.mail.Session;
  4. import jakarta.mail.Transport;
  5. import jakarta.mail.internet.InternetAddress;
  6. import jakarta.mail.internet.MimeMessage;
  7. import java.util.Properties;
  8. public class SendMail {
  9. public static void main(String[] args) throws Exception {
  10. Properties properties = new Properties();
  11. properties.put("mail.transport.protocol", "smtp");
  12. properties.put("mail.smtp.host", "192.168.133.135");
  13. properties.put("mail.smtp.port", 465);
  14. properties.put("mail.smtp.auth", "true");
  15. properties.put("mail.smtp.ssl.enable", true);
  16. // properties.put("mail.smtp.starttls.enable", "true");
  17. properties.put("mail.debug", "true");
  18. Session session = Session.getInstance(properties);
  19. // 获取邮件对象
  20. Message message = new MimeMessage(session);
  21. //设置发件人邮箱地址
  22. message.setFrom(new InternetAddress("steven@st.com"));
  23. //设置收件人地址
  24. message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress("russell@st.com") });
  25. //设置邮件标题
  26. message.setSubject("这是第一封Java邮件");
  27. //设置邮件内容
  28. message.setText("内容为: 这是第一封java发送来的邮件。");
  29. //得到邮差对象
  30. try (Transport transport = session.getTransport();){
  31. //连接自己的邮箱账户
  32. transport.connect("steven@st.com", "123456");//密码为刚才得到的授权码
  33. //发送邮件
  34. transport.sendMessage(message, message.getAllRecipients());
  35. }
  36. }
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/95460
推荐阅读
相关标签
  

闽ICP备14008679号