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();
}
}
}
JavaMail简单接收邮件
最新推荐文章于 2024-04-18 05:40:59 发布