本文整理匯總了Java中javax.mail.Store.getFolder方法的典型用法代碼示例。如果您正苦於以下問題:Java Store.getFolder方法的具體用法?Java Store.getFolder怎麽用?Java Store.getFolder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.Store
的用法示例。
在下文中一共展示了Store.getFolder方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initInbox
import javax.mail.Store; //導入方法依賴的package包/類
private static void initInbox() {
try {
System.out.println("Inbox initiating...");
Properties props = new Properties();
props.setProperty(MAIL_STORE_PROTOCOL_KEY, mailConfig.getProperty(MAIL_STORE_PROTOCOL_KEY));
// Used gmail with lesser secure authentication (https://www.google.com/settings/security/lesssecureapps)
// For a full access I should implement OAuth2 for ArdulinkMail (https://java.net/projects/javamail/pages/OAuth2)
Session session = Session.getInstance(props, null);
Store store = session.getStore();
System.out.println(mailConfig.getProperty(MAIL_HOST_KEY) + " " + mailConfig.getProperty(MAIL_USER_KEY) + " " + mailConfig.getProperty(MAIL_PASSWORD_KEY));
store.connect(mailConfig.getProperty(MAIL_HOST_KEY), mailConfig.getProperty(MAIL_USER_KEY), mailConfig.getProperty(MAIL_PASSWORD_KEY));
// clearDefault(store.getDefaultFolder()); // With GMAIL it doesn't work since "all messages" cannot be cleared.
clearMainFolder(store.getFolder("INBOX"));
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
System.out.println("Inbox initiated");
} catch (Exception e) {
throw new IllegalStateException("Error initiating inbox. Exiting...");
}
}
示例2: createTestMessages
import javax.mail.Store; //導入方法依賴的package包/類
public void createTestMessages() {
try {
GreenMailUser user = greenMail.setUser(TEST_RECIP, "password");
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
user.deliver(createMimeMessage("[email protected]", subject, body, null));
user.deliver(createMimeMessage("[email protected]", "Great stuff cheap!", "body", null));
user.deliver(createMimeMessage("[email protected]", "Happening this month", "stuff", null));
user.deliver(createMimeMessage("[email protected]", "Get certified today!", "Cheap!", null));
user.deliver(createMimeMessage("[email protected]", "It's been a long time!", "Let's talk", null));
user.deliver(createMimeMessage("[email protected]", "The Sound of Drums", "boom boom boom",
Date.from(LocalDate.now().minusYears(2).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())));
Session imapSession = greenMail.getImap().createSession();
Store store = imapSession.getStore("imap");
store.connect(TEST_RECIP, "password");
Folder defaultFolder = store.getDefaultFolder();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Folder ads = defaultFolder.getFolder("Ads");
ads.create(Folder.HOLDS_MESSAGES);
Folder spam = defaultFolder.getFolder("Spam");
spam.create(Folder.HOLDS_MESSAGES);
} catch (MessagingException ex) {
Logger.getLogger(TestMailUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例3: isMailReceived
import javax.mail.Store; //導入方法依賴的package包/類
/**
* Check whether email received by reading the emails.
*
* @param protocol to connect to the store
* @param user whose mail store should be connected
* @param subject the subject of the mail to search
* @return
* @throws MessagingException when unable to connect to the store
*/
public static boolean isMailReceived(String protocol, GreenMailUser user, String subject)
throws MessagingException {
Store store = getConnection(user, protocol);
Folder folder = store.getFolder(EMAIL_INBOX);
folder.open(Folder.READ_ONLY);
boolean isReceived = false;
Message[] messages = folder.getMessages();
for (Message message : messages) {
if (message.getSubject().contains(subject)) {
log.info("Found the Email with Subject : " + subject);
isReceived = true;
break;
}
}
return isReceived;
}
示例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: prepareMailbox
import javax.mail.Store; //導入方法依賴的package包/類
private void prepareMailbox() throws Exception {
// connect to mailbox
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("pop3");
store.connect("localhost", 25, "jones", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts 5 new messages
Message[] messages = new Message[5];
for (int i = 0; i < 5; i++) {
messages[i] = new MimeMessage(sender.getSession());
messages[i].setText("Message " + i);
messages[i].setHeader("Message-ID", "myuid-" + i);
}
folder.appendMessages(messages);
folder.close(true);
}
示例6: prepareMailbox
import javax.mail.Store; //導入方法依賴的package包/類
private void prepareMailbox() throws Exception {
// connect to mailbox
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("pop3");
store.connect("localhost", 25, "jones", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts 5 new messages
Message[] messages = new Message[5];
for (int i = 0; i < 5; i++) {
messages[i] = new MimeMessage(sender.getSession());
messages[i].setText("Message " + i);
messages[i].setHeader("Message-ID", "" + i);
}
folder.appendMessages(messages);
folder.close(true);
}
示例7: prepareMailbox
import javax.mail.Store; //導入方法依賴的package包/類
private void prepareMailbox(String user, String type) throws Exception {
// connect to mailbox
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore(type);
store.connect("localhost", 25, user, "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
InputStream is = getClass().getResourceAsStream("/SignedMailTestCaseHurz.elm");
Message hurzMsg = new MimeMessage(sender.getSession(), is);
Message[] messages = new Message[] {hurzMsg};
// insert one signed message
folder.appendMessages(messages);
folder.close(true);
}
示例8: 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();
}
示例9: 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();
}
}
}
示例10: checkNewMessage
import javax.mail.Store; //導入方法依賴的package包/類
public void checkNewMessage() {
logger.debug("check new message...");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect(user, password);
Folder inbox = store.getFolder(checkFolder);
inbox.open(Folder.READ_WRITE);
for (javax.mail.Message msg : inbox.getMessages()) {
processMail(msg);
}
// inbox.
} catch (Exception mex) {
mex.printStackTrace();
}
}
示例11: 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;
}
示例12: 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();
}
示例13: prepareMailbox
import javax.mail.Store; //導入方法依賴的package包/類
private void prepareMailbox() throws Exception {
// connect to mailbox
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("pop3");
store.connect("localhost", 25, "bill", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts 3 messages, one with earlier, one with later sent date and one with invalid subject (not returned in search)
Message[] messages = new Message[3];
messages[0] = new MimeMessage(sender.getSession());
messages[0].setText("Earlier date");
messages[0].setHeader("Message-ID", "0");
messages[0].setSentDate(new Date(10000));
messages[0].setSubject("Camel");
messages[1] = new MimeMessage(sender.getSession());
messages[1].setText("Later date");
messages[1].setHeader("Message-ID", "1");
messages[1].setSentDate(new Date(20000));
messages[1].setSubject("Camel");
messages[2] = new MimeMessage(sender.getSession());
messages[2].setText("Even later date");
messages[2].setHeader("Message-ID", "2");
messages[2].setSentDate(new Date(30000));
messages[2].setSubject("Invalid");
folder.appendMessages(messages);
folder.close(true);
}
示例14: _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;
}
示例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", "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();
}