日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java mail 已发送_JavaMail获取已发送邮件

發布時間:2025/3/11 java 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mail 已发送_JavaMail获取已发送邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

public static voidmain(String args[]) {

Properties props= new Properties(); //參數配置

props.setProperty("mail.transport.protocol", "smtp"); //使用的協議(JavaMail規范要求)

props.setProperty("mail.smtp.host", "smtp.exmail.qq.com"); //發件人的郵箱的SMTP服務器地址

props.setProperty("mail.smtp.auth", "true"); //需要請求認證//PS:某些郵箱服務器要求SMTP連接需要使用SSL安全認證(為了提高安全性,郵箱支持SSL連接,也可以自己開啟),//如果無法連接郵件服務器,仔細查看控制臺打印的 log,如果有有類似"連接失敗,要求 SSL安全連接"等錯誤,//開啟 SSL安全連接//SMTP服務器的端口(非 SSL連接的端口一般默認為 25,可以不添加,如果開啟了SSL連接,//需要改為對應郵箱的SMTP服務器的端口,具體可查看對應郵箱服務的幫助,//QQ郵箱的SMTP(SLL)端口為465或587

final String smtpPort = "465";

props.setProperty("mail.smtp.port", smtpPort);

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.setProperty("mail.smtp.socketFactory.fallback", "false");

props.setProperty("mail.smtp.socketFactory.port", smtpPort);

Session session=Session.getDefaultInstance(props);

session.setDebug(false);try{

Store store= session.getStore("imap");

store.connect("imap.exmail.qq.com", "it01@tofba.com", "*****");//change the user and password accordingly

Folder folder = store.getFolder("inbox");if (!folder.exists()) {

System.out.println("inbox not found");

System.exit(0);

}

folder.open(Folder.READ_ONLY);

Message[] messages=folder.getMessages();if (messages == null || messages.length <= 0) {

System.out.println("this inbox no messages");

System.exit(0);

}for(Message message : messages) {

String subject=message.getSubject();if(StringUtils.isNotBlank(subject)) {

System.out.println("subject:" +subject);

}/** 解析郵件內容*/Object content=message.getContent();if (null !=content) {if (content instanceofMimeMultipart) {

MimeMultipart multipart=(MimeMultipart)content;

parseMultipart(multipart);

}

}

}

}catch(Exception e) {

e.printStackTrace();

}

}/*** 對復雜郵件的解析

*

*@parammultipart

*@throwsMessagingException

*@throwsIOException*/

public static void parseMultipart(Multipart multipart) throwsMessagingException, IOException {int count =multipart.getCount();for (int idx = 0; idx < count; idx++) {

BodyPart bodyPart=multipart.getBodyPart(idx);

System.out.println(bodyPart.getContentType());if (bodyPart.isMimeType("text/plain")) {

System.out.println(bodyPart.getContent());

}else if (bodyPart.isMimeType("text/html")) {

System.out.println(bodyPart.getContent());

}else if (bodyPart.isMimeType("multipart/*")) {

Multipart mpart=(Multipart)bodyPart.getContent();

parseMultipart(mpart);

}else if (bodyPart.isMimeType("application/octet-stream")) {

String disposition=bodyPart.getDisposition();

System.out.println(disposition);if (StringUtils.isNotBlank(disposition) &&disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {

String fileName=bodyPart.getFileName();

InputStream is=bodyPart.getInputStream();

copy(is,new FileOutputStream("D:\\tofba\\email\\" +fileName));

}

}

}

}/*** 文件拷貝,在用戶進行附件下載的時候,可以把附件的InputStream傳給用戶進行下載

*

*@paramis

*@paramos

*@throwsIOException*/

public static voidcopy(InputStream is, OutputStream os)throwsIOException {byte[] bytes = new byte[1024];int len = 0;while ((len = is.read(bytes)) != -1) {

os.write(bytes,0, len);

}if (os != null)

os.close();if (is != null)

is.close();

}

總結

以上是生活随笔為你收集整理的java mail 已发送_JavaMail获取已发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。