本文整理汇总了Java中javax.mail.Store.close方法的典型用法代码示例。如果您正苦于以下问题:Java Store.close方法的具体用法?Java Store.close怎么用?Java Store.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Store
的用法示例。
在下文中一共展示了Store.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: closeStoreConnection
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Close connection
* <b>Note</b>Internal method
* @throws MessagingException
*/
public void closeStoreConnection(
boolean storeConnected ) throws MessagingException {
if (storeConnected) {
// the folder is empty when the message is not loaded from IMAP server, but from a file
Folder imapFolder = message.getFolder();
if (imapFolder == null) {
imapFolder = partOfImapFolder; // in case of nested package but still originating from IMAP server
}
if (imapFolder != null) {
Store store = imapFolder.getStore();
if (store != null && store.isConnected()) {
// closing store closes and its folders
log.debug("Closing store (" + store.toString() + ") and associated folders");
store.close();
}
}
}
}
示例2: isMailReceivedBySubject
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Check mail folder for an email using subject.
*
* @param emailSubject Email subject
* @param folder mail folder to check for an email
* @param protocol protocol used to connect to the server
* @return whether mail received or not
* @throws MessagingException if we're unable to connect to the store
*/
private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol,
GreenMailUser user) throws MessagingException {
boolean emailReceived = false;
Folder mailFolder;
Store store = getConnection(user, protocol);
try {
mailFolder = store.getFolder(folder);
mailFolder.open(Folder.READ_WRITE);
SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
Message[] messages = mailFolder.search(searchTerm);
for (Message message : messages) {
if (message.getSubject().contains(emailSubject)) {
log.info("Found the Email with Subject : " + emailSubject);
emailReceived = true;
break;
}
}
} finally {
if (store != null) {
store.close();
}
}
return emailReceived;
}
示例3: deleteAllEmails
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Delete all emails in the inbox.
*
* @param protocol protocol used to connect to the server
* @throws MessagingException if we're unable to connect to the store
*/
public static void deleteAllEmails(String protocol, GreenMailUser user) throws MessagingException {
Folder inbox = null;
Store store = getConnection(user, protocol);
try {
inbox = store.getFolder(EMAIL_INBOX);
inbox.open(Folder.READ_WRITE);
Message[] messages = inbox.getMessages();
for (Message message : messages) {
message.setFlag(Flags.Flag.DELETED, true);
log.info("Deleted email Subject : " + message.getSubject());
}
} finally {
if (inbox != null) {
inbox.close(true);
}
if (store != null) {
store.close();
}
}
}
示例4: assertConnection
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Asserts that a connection to the given mailbox is possible.
*
* @param protocol
* The protocol.
* @param isStartTls
* True, if StartTLS ist possible.
* @param host
* The host.
* @param port
* The port.
* @param login
* The login.
* @param password
* The password.
* @param mailbox
* The mailbox.
* @throws ConnectionException
* Exception.
* @throws MessagingException
* Exception.
*/
private void assertConnection(String protocol, boolean isStartTls, String host, String port,
String login, String password, String mailbox) throws ConnectionException,
MessagingException {
String propertiesProtocolPrefix = "mail." + protocol + ".";
Properties properties = new Properties();
properties.putAll(System.getProperties());
properties.put(propertiesProtocolPrefix + "starttls.enable", isStartTls);
Session session = Session.getInstance(properties);
Store store = session.getStore(protocol);
try {
store.connect(host, NumberUtils.toInt(port, -1), login, password);
Folder folder = store.getFolder(mailbox);
if (!folder.exists()) {
throw new FolderNotFoundException();
}
} finally {
store.close();
}
}
示例5: checkInboxSave
import javax.mail.Store; //导入方法依赖的package包/类
public void checkInboxSave(int mode) throws MessagingException, IOException {
Store store = session.getStore();
store.connect();
Folder root = store.getDefaultFolder();
Folder inbox = root.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
for (Message msg2 : msgs) {
POP3Message msg = (POP3Message) msg2;
Object object = msg.getContent();
if (object instanceof Multipart) {
Multipart multipart = (Multipart) object;
for (int i = 0, n = multipart.getCount(); i < n; i++) {
MailClient.handlePart(multipart.getBodyPart(i));
}
}
System.out.println(" From: " + msg.getFrom()[0]);
System.out.println(" Subject: " + msg.getSubject());
System.out.println(" Content: " + object);
}
inbox.close(true);
store.close();
}
示例6: checkInbox
import javax.mail.Store; //导入方法依赖的package包/类
public void checkInbox(int mode) throws MessagingException, IOException {
if (mode == 0) {
return;
}
boolean show = (mode & MailClient.SHOW_MESSAGES) > 0;
boolean clear = (mode & MailClient.CLEAR_MESSAGES) > 0;
String action = (show ? "Show" : "") + (show && clear ? " and " : "") + (clear ? "Clear" : "");
System.out.println(action + " INBOX for " + from);
Store store = session.getStore();
store.connect();
Folder root = store.getDefaultFolder();
Folder inbox = root.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
if (msgs.length == 0 && show) {
System.out.println("No messages in inbox");
}
for (Message msg2 : msgs) {
MimeMessage msg = (MimeMessage) msg2;
if (show) {
System.out.println(" From: " + msg.getFrom()[0]);
System.out.println(" Subject: " + msg.getSubject());
System.out.println(" Content: " + msg.getContent());
}
if (clear) {
msg.setFlag(Flags.Flag.DELETED, true);
}
}
inbox.close(true);
store.close();
System.out.println();
}
示例7: testAllServices
import javax.mail.Store; //导入方法依赖的package包/类
@Test
public void testAllServices() throws MessagingException {
// Send messages via SMTP and secure SMTPS
GreenMailUtil.sendTextEmail("[email protected]", "[email protected]",
"test1", "Test GreenMail Docker service",
ServerSetupTest.SMTP.createCopy(bindAddress));
GreenMailUtil.sendTextEmail("[email protected]", "[email protected]",
"test2", "Test GreenMail Docker service",
ServerSetupTest.SMTPS.createCopy(bindAddress));
// IMAP
for (ServerSetup setup : Arrays.asList(
ServerSetupTest.IMAP.createCopy(bindAddress),
ServerSetupTest.IMAPS.createCopy(bindAddress),
ServerSetupTest.POP3.createCopy(bindAddress),
ServerSetupTest.POP3S.createCopy(bindAddress))) {
final Store store = Session.getInstance(setup.configureJavaMailSessionProperties(null, false)).getStore();
store.connect("[email protected]", "[email protected]");
try {
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
assertEquals("Can not check mails using "+store.getURLName(), 2, folder.getMessageCount());
} finally {
store.close();
}
}
}
示例8: closeStore
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Handy method to do the try catch try of closing a mail store and folder.
* @param mailStore
* @param mailFolder
*/
private void closeStore(final Store mailStore, final Folder mailFolder) {
try {
if (mailFolder != null && mailFolder.isOpen()) {
mailFolder.close(true);
}
} catch (final MessagingException e) {
LogUtils.debugf(this, e, "Unable to close mail folder.");
} finally {
try {
if (mailStore != null && mailStore.isConnected()) {
mailStore.close();
}
} catch (final MessagingException e1) {
LogUtils.debugf(this, e1, "Unable to close message store.");
}
}
}
示例9: deleteMailsFromUserMailbox
import javax.mail.Store; //导入方法依赖的package包/类
protected void deleteMailsFromUserMailbox(final Properties props, final String folderName, final int start, final int deleteCount,
final String user, final String password) throws MessagingException {
final Store store = Session.getInstance(props).getStore();
store.connect(user, password);
checkStoreForTestConnection(store);
final Folder f = store.getFolder(folderName);
f.open(Folder.READ_WRITE);
final int msgCount = f.getMessageCount();
final Message[] m = deleteCount == -1 ? f.getMessages() : f.getMessages(start, Math.min(msgCount, deleteCount + start - 1));
int d = 0;
for (final Message message : m) {
message.setFlag(Flag.DELETED, true);
logger.info("Delete msgnum: {} with sid {}", message.getMessageNumber(), message.getSubject());
d++;
}
f.close(true);
logger.info("Deleted " + d + " messages");
store.close();
}
示例10: getEmail
import javax.mail.Store; //导入方法依赖的package包/类
public Mail getEmail(int mailId, MailCredentials credentials, String salt) throws MessagingException, IOException {
Properties properties = getServerProperties(credentials.getProtocol(), credentials.getHost(),
"" + credentials.getPort());
Session session = Session.getDefaultInstance(properties);
// connects to the message store
Store store = session.getStore(credentials.getProtocol());
store.connect(credentials.getUsername(), credentials.getPassword());
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
// fetches new messages from server
int msgnum[] = new int[] { mailId };
Message[] messages = folderInbox.getMessages(msgnum);
Mail mail = getMails(messages).get(0);
// disconnect
folderInbox.close(false);
store.close();
return mail;
}
示例11: changeMailStatus
import javax.mail.Store; //导入方法依赖的package包/类
public void changeMailStatus(int mailId, Flag flag, MailCredentials credentials) throws MessagingException {
Properties properties = getServerProperties(credentials.getProtocol(), credentials.getHost(),
"" + credentials.getPort());
Session session = Session.getDefaultInstance(properties);
// connects to the message store
Store store = session.getStore(credentials.getProtocol());
store.connect(credentials.getUsername(), credentials.getPassword());
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);
logger.debug("unread:" + folderInbox.getUnreadMessageCount() + ", new:" + folderInbox.getNewMessageCount());
// fetches new messages from server
int msgnum[] = new int[] { mailId };
Message[] messages = folderInbox.getMessages(msgnum);
if (messages.length == 1 && messages[0] != null) {
messages[0].setFlag(flag, true);
}
folderInbox.close(false);
store.close();
}
示例12: _getEmail
import javax.mail.Store; //导入方法依赖的package包/类
public MessageWrapper _getEmail( String hostName, MessageFilter[] messageFilters, Map<String, String> propertyMap )
{
Properties mailProps = new Properties();
mailProps.putAll( propertyMap );
List<Message> messageList = new ArrayList<Message>( 10 );
MessageWrapper messageWrapper = null;
try
{
Session emailSession = Session.getDefaultInstance( mailProps );
Store store = emailSession.getStore( propertyMap.get( PROTOCOL ) );
store.connect( hostName, propertyMap.get( USER_NAME ), propertyMap.get( PASSWORD ) );
Folder emailFolder = store.getFolder( propertyMap.get( FOLDER_NAME ) != null ? propertyMap.get( FOLDER_NAME ) : DEFAULT_FOLDER_NAME );
emailFolder.open( Folder.READ_WRITE );
Message[] messages = emailFolder.getMessages();
int messageCount = emailFolder.getMessageCount();
if ( messageCount > MAX_MESSAGES )
messageCount = MAX_MESSAGES;
if ( log.isInfoEnabled() )
log.info( "Processing " + messageCount + " messages" );
for ( int i = 0; i < messageCount; i++ )
{
if ( applyFilters( messages[i], messageFilters ) )
{
messageList.add( messages[i] );
}
}
if ( messageList.size() == 0 )
throw new ScriptException( "Failed to find any email messages that met the criteria" );
Collections.sort( messageList, new DateComparator() );
String messageContent = null;
String contentType = messageList.get( 0 ).getContentType();
if ( contentType.startsWith( "text/" ) )
messageContent = messageList.get( 0 ).getContent().toString();
else if ( messageList.get( 0 ).isMimeType( "multipart/*" ) )
messageContent = getTextFromMimeMultipart( (MimeMultipart) messageList.get( 0 ).getContent() );
else
messageContent = messageList.get( 0 ).getContent().getClass().getName();
messageWrapper = new MessageWrapper( messageList.size(), messageList.get( 0 ).getFrom()[0].toString(), messageList.get( 0 ).getSubject(), messageContent, contentType );
emailFolder.close( false );
store.close();
}
catch ( Exception e )
{
e.printStackTrace();
throw new ScriptException( "Failed to find email" );
}
return messageWrapper;
}
示例13: receive
import javax.mail.Store; //导入方法依赖的package包/类
/**
* �����ʼ�
*/
public static void receive() throws Exception {
// �����ӷ������ĻỰ��Ϣ
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3"); // ��
props.setProperty("mail.pop3.port", "110"); // �˿�
props.setProperty("mail.pop3.host", "pop3.sina.com"); // pop3������
// ����Sessionʵ������
Session session = Session.getInstance(props);
Store store = session.getStore("pop3");
store.connect("[email protected]", "mh899821671104");
// ����ռ���
Folder folder = store.getFolder("INBOX");
/* Folder.READ_ONLY��ֻ��Ȩ��
* Folder.READ_WRITE���ɶ���д���������ʼ���״̬��
*/
folder.open(Folder.READ_WRITE); //���ռ���
// ����POP3Э������֪�ʼ���״̬,����getUnreadMessageCount�õ������ռ�����ʼ�����
System.out.println("δ���ʼ���: " + folder.getUnreadMessageCount());
// ����POP3Э������֪�ʼ���״̬,��������õ��Ľ��ʼ�ն���Ϊ0
System.out.println("ɾ���ʼ���: " + folder.getDeletedMessageCount());
System.out.println("���ʼ�: " + folder.getNewMessageCount());
// ����ռ����е��ʼ�����
System.out.println("�ʼ�����: " + folder.getMessageCount());
// �õ��ռ����е������ʼ�,������
Message[] messages = folder.getMessages();
parseMessage(messages);
//�ͷ���Դ
folder.close(true);
store.close();
}
示例14: main
import javax.mail.Store; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Properties props = new Properties();
String host = "pop.mailinator.com";
String username = "x-test-m";
String password = "mypwdxxx";
String provider = "pop3";
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
store.connect(host, username, password);
Folder inbox = store.getFolder("INBOX");
if (inbox == null) {
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
messages[i].writeTo(System.out);
}
inbox.close(false);
store.close();
}
示例15: receive
import javax.mail.Store; //导入方法依赖的package包/类
/**
* 接收邮件
*/
public static void receive() throws Exception {
// 准备连接服务器的会话信息
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3"); // 协议
props.setProperty("mail.pop3.port", "995"); // 端口
props.setProperty("mail.pop3.host", "pop3.live.com"); // pop3服务器
// 创建Session实例对象
Session session = Session.getInstance(props);
Store store = session.getStore("pop3");
store.connect("[email protected]", "Mo8yOU8u");
// 获得收件箱
Folder folder = store.getFolder("INBOX");
/* Folder.READ_ONLY:只读权限
* Folder.READ_WRITE:可读可写(可以修改邮件的状态)
*/
folder.open(Folder.READ_WRITE); //打开收件箱
// 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
// 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
System.out.println("新邮件: " + folder.getNewMessageCount());
// 获得收件箱中的邮件总数
System.out.println("邮件总数: " + folder.getMessageCount());
// 得到收件箱中的所有邮件,并解析
Message[] messages = folder.getMessages();
parseMessage(messages);
//释放资源
folder.close(true);
store.close();
}