java删除邮件_JavaMail删除服务器邮件

package com.what21.network.mail;

import java.util.Properties;

import javax.mail.Flags;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.NoSuchProviderException;

import javax.mail.Session;

import javax.mail.Store;

public class EmailMessageRemover {

/**

* Deletes all e-mail messages whose subject field contain a string

* specified by 'subjectToDelete'

*

* @param host

* @param port

* @param userName

* @param password

* @param subjectToDelete delete if the message's subject contains this value.

*/

public void deleteMessages(String host, String port, String userName,

String password, String subjectToDelete) {

Properties properties = new Properties();

// server setting

properties.put("mail.imap.host", host);

properties.put("mail.imap.port", port);

// SSL setting

properties.setProperty("mail.imap.socketFactory.class",

"javax.net.ssl.SSLSocketFactory");

properties.setProperty("mail.imap.socketFactory.fallback", "false");

properties.setProperty("mail.imap.socketFactory.port",

String.valueOf(port));

Session session = Session.getDefaultInstance(properties);

try {

// connects to the message store

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

store.connect(userName, password);

// opens the inbox folder

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

folderInbox.open(Folder.READ_WRITE);

// fetches new messages from server

Message[] arrayMessages = folderInbox.getMessages();

for (int i = 0; i < arrayMessages.length; i++) {

Message message = arrayMessages[i];

String subject = message.getSubject();

if (subject.contains(subjectToDelete)) {

message.setFlag(Flags.Flag.DELETED, true);

System.out.println("Marked DELETE for message: " + subject);

}

}

// expunges the folder to remove messages which are marked deleted

boolean expunge = true;

folderInbox.close(expunge);

// another way:

// folderInbox.expunge();

// folderInbox.close(false);

// disconnect

store.close();

} catch (NoSuchProviderException ex) {

System.out.println("No provider.");

ex.printStackTrace();

} catch (MessagingException ex) {

System.out.println("Could not connect to the message store.");

ex.printStackTrace();

}

}

/**

* Runs this program to delete e-mail messages on a Gmail account via IMAP

* protocol.

*/

public static void main(String[] args) {

String host = "imap.gmail.com";

String port = "993";

String userName = "your_email";

String password = "your_password";

EmailMessageRemover remover = new EmailMessageRemover();

// try to delete all messages contain this string its Subject field

String subjectToDelete = "Newsletter";

remover.deleteMessages(host, port, userName, password, subjectToDelete);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值