JavaMail简单接收邮件

package com.example.emailback.controller;

import com.example.emailback.pojo.Receive;
import com.example.emailback.util.Result;
import com.example.emailback.util.StatusCode;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.*;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

public class MailReceives {
    public static void main(String[] args) {
        String pop3Server = "pop3.163.com";
        String protocol = "pop3";
        String username = "xxxx";
        String password = "xxxx";
        Properties prop = new Properties();
        prop.setProperty("mail.store.protocol", protocol);
        prop.setProperty("mail.pop3.host", pop3Server);
        Session mailSession = Session.getInstance(prop,null);
        mailSession.setDebug(false);
        try {
            Store store = mailSession.getStore(protocol);
            //登录验证
            store.connect(pop3Server,username,password);
            //获取邮箱账户
            Folder folder = store.getFolder("inbox");
            //设置访问权限
            folder.open(Folder.READ_WRITE);
            //获取所有邮件
            Message[] messages = folder.getMessages();
            for(int i=messages.length - 1; i<messages.length; i++){
					//主题
                    String subject = messages[i].getSubject();
                    //时间
                    Date sendDate = messages[i].getSentDate();
                    //发件人
                    String from = messages[i].getFrom()[0].toString();
                    //内容
                    MimeMultipart part = (MimeMultipart) messages[i].getContent();
                    BodyPart body = part.getBodyPart(0);
                    String content = body.getContent().toString();
					
					String data = "<p>" + "主题:" + subject
                            + "<br/><br/>" + "发件人:" + from
                            + "<br/><br/>" + "时间:" + sendDate
                            + "<br/><br/><br/>" + "正文:"
                            + "<br/><br/>" + content
                            + "</p>";
                            
                System.out.println("第" + (i+1) + "封邮件的主题为:" + subject + "\t发件人地址为:" + from);
                System.out.println("你想阅读此邮件吗(y/n)?");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                String input = bufferedReader.readLine();
                if("y".equals(input.toLowerCase())){
                    messages[i].writeTo(System.out);
                }
            }
            folder.close(false);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值