java mail api,使用Java Mail API读取邮件内容

I am able to get the from field and subject field correctly , But i am getting the Mail Content in Object Format even though i m using toString() method..

please check with the following source code

try {

Session session = Session.getInstance(props, null);

Store store = session.getStore();

store.connect("imap.gmail.com", "mymail@gmail.com", "****");

Folder inbox = store.getFolder("INBOX");

inbox.open(Folder.READ_ONLY);

Message msg = inbox.getMessage(inbox.getMessageCount());

Address[] in = msg.getFrom();

for (Address address : in) {

System.out.println("FROM:" + address.toString());

}

Object obj = msg.getContent();

//Multipart mp = (Multipart)obj;

Multipart mp = (Multipart) msg.getContent();

// MimeBodyPart part = (MimeBodyPart)mp.getBodyPart(0);

BodyPart bp = ((Multipart) msg.getContent()).getBodyPart(0);

// Object body = msg.getContent();

//String value = String.valueOf(body);

System.out.println("SENT DATE:" + msg.getSentDate());

System.out.println("SUBJECT:" + msg.getSubject());

System.out.println("CONTENT:" + bp.getContent().toString());

} catch (Exception mex) {

mex.printStackTrace();

}

}

}

output console:

FROM:Myname

SENT DATE:Tue Nov 05 12:28:24 IST 2013

SUBJECT:test

CONTENT:javax.mail.internet.MimeMultipart@5117f31e

解决方案

You need to iterate through all multiparts, then check MIME type of the Part in order to know if you have to treat it like a text or an attachment.

for(int i=0;i

BodyPart bodyPart = multipart.getBodyPart(i);

if (bodyPart.isMimeType("text/*")) {

String s = (String) bodyPart.getContent();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要读取当天已读邮件,可以使用 Java Mail API 中的 IMAP 协议来实现。以下是示例代码: ```java import javax.mail.*; import javax.mail.internet.*; import java.util.*; public class ReadTodayReadEmails { public static void main(String[] args) throws Exception { // 设置邮件服务器参数 String host = "imap.gmail.com"; String username = "your_username"; String password = "your_password"; // 获取当天日期 Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); String today = sdf.format(cal.getTime()); // 连接到邮件服务器 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); props.setProperty("mail.imaps.host", host); props.setProperty("mail.imaps.port", "993"); props.setProperty("mail.imaps.ssl.enable", "true"); Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect(username, password); // 打开收件箱 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); // 获取当天已读邮件 Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), true)); List<Message> todayReadMessages = new ArrayList<Message>(); for (Message message : messages) { Date date = message.getSentDate(); String sentDate = sdf.format(date); if (sentDate.equals(today)) { todayReadMessages.add(message); } } // 输出邮件内容 for (Message message : todayReadMessages) { System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); System.out.println("To: " + message.getAllRecipients()[0]); System.out.println("Date: " + message.getSentDate()); System.out.println("Content: " + message.getContent().toString()); } // 关闭连接 inbox.close(false); store.close(); } } ``` 请将 `your_username` 和 `your_password` 替换为您的实际用户名和密码。另外,如果您使用的是其他邮件服务器,请修改 `host`、`mail.imaps.host` 和 `mail.imaps.port` 参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值