javaMail发送邮件获取SMTP状态码

    项目需要集成邮件发送,之前在.net下写过smtp的邮件发送,好像可以直接获取状态码。在网上找到的javamail发送例子中并未给出如何获取状态码。而且sendMessage方法直接是void没任何返回参数。

      之后在查阅api时终于有所发现

       When sending a message, detailed information on each address that fails is available in an SMTPAddressFailedException chained off the top level SendFailedException that is thrown. In addition, if the mail.smtp.reportsuccess property is set, anSMTPAddressSucceededException will be included in the list for each address that is successful. Note that this will cause a top level SendFailedException to be thrown even though the send was successful.

SMTPAddressFailedException 和SMTPAddressSucceededException 中就包含了状态码


以作备注,希望下次再遇到问题的时候自己能搜到。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
获取发送邮件,你需要连接到邮件服务器,打开“已发送”邮件夹,并遍历所有邮件。以下是一个简单的示例代码: ```java // 连接到邮件服务器 Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.example.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_username", "your_password"); } }); Store store = session.getStore("imap"); store.connect("imap.example.com", "your_username", "your_password"); // 打开“已发送”邮件夹 Folder folder = store.getFolder("Sent"); folder.open(Folder.READ_ONLY); // 遍历所有邮件 Message[] messages = folder.getMessages(); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; System.out.println("Subject: " + message.getSubject()); } // 关闭邮件夹和连接 folder.close(false); store.close(); ``` 需要注意的是,不同的邮件服务器可能会有不同的邮件夹名称,你需要根据你的邮件服务器来设置相应的名称,或者使用JavaMail提供的`list()`方法来列出所有的邮件夹名称。 此外,你也可以使用搜索条件(如发件人、收件人、主题等)来过滤邮件,只获取你需要的邮件。例如: ```java SearchTerm searchTerm = new SubjectTerm("Testing JavaMail"); Message[] messages = folder.search(searchTerm); ``` 这将只获取主题为“Testing JavaMail”的邮件。你可以根据自己的需求,设置不同的搜索条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值