use of javax.mail.URLName in project gocd by gocd.
the class Pop3MailClient method getInboxFolder.
private Folder getInboxFolder() throws MessagingException {
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.host", host);
Authenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(pop3Props, auth);
URLName url = new URLName("pop3", host, port, "", username, password);
Store store = session.getStore(url);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
return folder;
}
use of javax.mail.URLName in project Gargoyle by callakrsos.
the class MailPopReceiver method receiveEmail.
public void receiveEmail() {
try {
//1) get the session object
//new Properties();
Properties properties = System.getProperties();
properties.put("mail.pop3.host", popHost);
properties.put("mail.user", userName);
properties.put("mail.from", hostEmailAddr);
properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.pop3.socketFactory.port", "995");
properties.put("mail.pop3.port", "995");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
if (this.proxy != null) {
String hostName = proxy.getHostName();
int port = proxy.getPort();
String value = port + "";
properties.put("proxySet", "true");
properties.put("socksProxyHost", hostName);
properties.put("socksProxyPort", value);
properties.put("http.proxyHost", hostName);
properties.put("http.proxyPort", port + "");
properties.put("https.proxyHost", hostName);
properties.put("https.proxyPort", port + "");
//#####################################
properties.setProperty("proxySet", "true");
properties.setProperty("socksProxyHost", hostName);
properties.setProperty("socksProxyPort", port + "");
properties.setProperty("http.proxyHost", hostName);
properties.setProperty("http.proxyPort", port + "");
properties.setProperty("https.proxyHost", hostName);
properties.setProperty("https.proxyPort", port + "");
}
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(hostEmailAddr, password);
}
};
Session emailSession = Session.getInstance(properties, authenticator);
// Session emailSession = Session.getDefaultInstance(properties);
emailSession.setDebug(debug);
//2) create the POP3 store object and connect with the pop server
// POP3Store emailStore = (POP3Store) emailSession.getStore(type);
URLName url = new URLName(type, popHost, 995, "", hostEmailAddr, password);
POP3SSLStore emailStore = new POP3SSLStore(emailSession, url);
System.out.println(emailStore.isSSL());
emailStore.connect();
System.out.println("connected.");
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
//4) retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
//5) close the store and folder objects
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of javax.mail.URLName in project JMRI by JMRI.
the class MailMessage method saveCopy.
public void saveCopy() {
try {
if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null) {
store = session.getStore(protocol);
} else {
store = session.getStore();
}
// Connect
if (host != null || user != null || password != null) {
store.connect(host, user, password);
} else {
store.connect();
}
}
// Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
log.error("Can't get record folder!");
} else {
if (!folder.exists()) {
folder.create(Folder.HOLDS_MESSAGES);
}
Message[] msgs = new Message[1];
msgs[0] = msg;
folder.appendMessages(msgs);
log.info("Mail was recorded successfully.");
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
Aggregations