本文整理匯總了Java中javax.mail.Store類的典型用法代碼示例。如果您正苦於以下問題:Java Store類的具體用法?Java Store怎麽用?Java Store使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Store類屬於javax.mail包,在下文中一共展示了Store類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getEmail
import javax.mail.Store; //導入依賴的package包/類
public void getEmail() throws MessagingException, IOException {
Session session = Session.getDefaultInstance(System.getProperties(),null);
Store store = null;
store = session.getStore("imaps");
// store.connect(this.host, this.userName, this.password);
// Get default folder
Folder folder = store.getDefaultFolder();
folder.getMessages();
folder.getNewMessageCount();
Message m = folder.getMessage(0);
m.getMessageNumber();
m.getAllRecipients();
m.getReceivedDate();
m.getFrom();
m.getSubject();
m.getReplyTo();
m.getContent();
m.getSize();
// Get any folder by name
Folder[] folderList = folder.list();
}
示例2: 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();
}
}
}
}
示例3: 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;
}
示例4: 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].setHeader("Message-ID", "" + i);
messages[i].setText("Message " + i);
}
folder.appendMessages(messages);
folder.close(true);
}
示例5: 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...");
}
}
示例6: reconnectStoreIfClosed
import javax.mail.Store; //導入依賴的package包/類
/**
* Reconnects if connection is closed.
* <b>Note</b>Internal method
* @return true if store re-connection is performed and this means that close should be closed after the work is done
* @throws MessagingException
*/
public boolean reconnectStoreIfClosed() throws MessagingException {
boolean storeReconnected = false;
// 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 = this.partOfImapFolder;
} else {
partOfImapFolder = imapFolder; // keep reference
}
if (imapFolder != null) {
Store store = imapFolder.getStore();
if (store != null) {
if (!store.isConnected()) {
log.debug("Reconnecting store... ");
store.connect();
storeReconnected = true;
}
// Open folder in read-only mode
if (!imapFolder.isOpen()) {
log.debug("Reopening folder " + imapFolder.getFullName()
+ " in order to get contents of mail message");
imapFolder.open(Folder.READ_ONLY);
}
}
}
return storeReconnected;
}
示例7: 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);
}
}
示例8: 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();
}
}
}
示例9: 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;
}
示例10: 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();
}
}
示例11: 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 1 new message
Message[] messages = new Message[1];
messages[0] = new MimeMessage(sender.getSession());
messages[0].setSubject("TestSubject");
messages[0].setHeader("Message-ID", "0");
messages[0].setText("TestText");
folder.appendMessages(messages);
folder.close(true);
}
示例12: 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("imap");
store.connect("localhost", 25, "claus", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts two new messages
Message[] msg = new Message[2];
msg[0] = new MimeMessage(sender.getSession());
msg[0].setText("Message 1");
msg[0].setHeader("Message-ID", "0");
msg[0].setFlag(Flags.Flag.SEEN, false);
msg[1] = new MimeMessage(sender.getSession());
msg[1].setText("Message 2");
msg[0].setHeader("Message-ID", "1");
msg[1].setFlag(Flags.Flag.SEEN, true);
folder.appendMessages(msg);
folder.close(true);
}
示例13: 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);
}
示例14: 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);
}
示例15: 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("imap");
store.connect("localhost", 25, "claus", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts two messages with the SEEN flag
Message[] msg = new Message[2];
msg[0] = new MimeMessage(sender.getSession());
msg[0].setText("Message 1");
msg[0].setHeader("Message-ID", "0");
msg[0].setFlag(Flags.Flag.SEEN, true);
msg[1] = new MimeMessage(sender.getSession());
msg[1].setText("Message 2");
msg[0].setHeader("Message-ID", "1");
msg[1].setFlag(Flags.Flag.SEEN, true);
folder.appendMessages(msg);
folder.close(true);
}