本文整理汇总了Java中javax.mail.Store.connect方法的典型用法代码示例。如果您正苦于以下问题:Java Store.connect方法的具体用法?Java Store.connect怎么用?Java Store.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Store
的用法示例。
在下文中一共展示了Store.connect方法的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: 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;
}
示例3: 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);
}
}
示例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].setText("Message " + i);
messages[i].setHeader("Message-ID", "myuid-" + i);
}
folder.appendMessages(messages);
folder.close(true);
}
示例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].setHeader("Message-ID", "" + i);
messages[i].setText("Message " + 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("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);
}
示例7: 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();
}
}
示例8: 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);
}
示例9: 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);
}
示例10: 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();
}
示例11: 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();
}
示例12: main
import javax.mail.Store; //导入方法依赖的package包/类
public static void main(String[] args) throws MessagingException {
String url = "https://outlook.office365.com/ews/exchange.asmx";
Properties properties = new Properties();
properties.setProperty("org.sourceforge.net.javamail4ews.util.Util.EnableServiceTrace", "true");
Session session = Session.getInstance(properties);
System.out.println("retrieving the store");
//Get the EWS store implementation
Store store = session.getStore("ewsstore");
System.out.println("done");
//Connect to the Exchange server - No port required.
//Also connect() might be used if the session is initalized with the known mail.* properties
System.out.println(String.format("connecting to %s", url));
store.connect(url,
"[email protected]",
"p4ssw0rd");
System.out.println(String.format("connected to %s", url));
System.out.println(String.format("get default folder", url));
Folder folder = store.getDefaultFolder();
System.out.println(String.format("open default folder as read-only"));
folder.open(Folder.READ_ONLY);
System.out.println(String.format("done"));
Message[] messages = folder.getMessages();
System.out.println(String.format("there is %d messages", messages.length));
System.out.println(messages[0].getFrom());
}
示例13: 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();
}
}
}
示例14: 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();
}
}
示例15: 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();
}