当前位置:   article > 正文

JavaMail简单接收邮件_javax.mail properties pop3接收邮件配置 csdn

javax.mail properties pop3接收邮件配置 csdn
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();
        }
    }
}
  • 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
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/95494
推荐阅读
相关标签
  

闽ICP备14008679号