How to download emails into local system using java? - java

I need to download gmail-> emails into my local system. Idea is to convert each 'email's' content into an XML and upload it onto some other system. I am wondering if there is an API which allows me to download 'my' e-mails from gmail account onto a local hard drive (e.g c:\gmail-mails)

Here is a code snipped I got from this site.
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
//TODO store the message cntent in anXML file
}
} catch (NoSuchProviderException e) {
e.printStackTrace();//...
} catch (MessagingException e) {
e.printStackTrace();//...
}
And maybe better, also have a look at the extention of JavaMail Google made.

Related

Authentication Error with Java Mail delegate access

I am trying to use delegate mail access through javamail. I have configured the delegate access correctly on my system. But when I try to run the following code
import java.util.Properties;
import javax.mail.*;
public class Test {
public static void main(String[] args) {
Properties props = System.getProperties();
props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.smtp.ssl.enable", "true");
try {
Session session = Session.getInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imaps");
store.connect(mailServerIP, 993, "myAlias/DelegateAlias", "mypassword");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for (Message message : messages) {
System.out.println(message);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
}
it shows me the following error
javax.mail.AuthenticationFailedException: LOGIN failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:715)
at javax.mail.Service.connect(Service.java:364)
at com.adecco.smpt.TestSSL.main(TestSSL.java:26)
If I try to access my mail box instead of shared mailbox everything works well. Is there a different syntax to access the SHARED MAILBOX in JAVA?
I am using the private mail server.

FTPSClient.listFiles() not working for NonStop/Tandem System

I am writing a small FTPS client that will download Enscribe files from NonStop/Tandem and will be processes in Windows. I am using the Apache Commons Net API to achieve this.
I am able to download and upload files from and to NonStop/Tandem. But I am not able to list the files and the directories using the listFiles() and/or mlistDir() methods present under org.apache.commons.net.ftp.FTPClient class.
Below is my code to list the files present in the current working directory.
FTPSClient client = new FTPSClient(false);
try {
client.connect(serverAddress, serverPort);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
if (client.login(userName, passwd)) {
System.out.println(client.getReplyString());
// Set protection buffer size
client.execPBSZ(0);
// Set data channel protection to private
client.execPROT("P");
// Enter local passive mode
client.enterLocalPassiveMode();
// Get Current Working Directory
client.printWorkingDirectory();
System.out.println(client.getReplyString());
FTPFile[] files = client.listFiles();
// Logout
client.logout();
System.out.println(client.getReplyString());
} else {
System.out.println("Login failed...");
}
// Disconnect from Server
client.disconnect();
System.out.println("Disconnected from Host...");
} else {
System.out.println("Connection to Host failed...");
System.out.println("Error Code - " + reply);
}
} catch (Exception e) {
e.printStackTrace();
}
I get the following error while executing the code:
org.apache.commons.net.ftp.parser.ParserInitializationException: Unknown parser type: Nonstop J-series Server : J06.19.
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.__createParser(FTPClient.java:3377)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3334)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3065)
at com.connect.ssl.FTPSTest.main(FTPSTest.java:57)
I even tried to set the FTPClient configuration as UNIX as below, but it didn't helped.
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
Can anyone help me with this.

Mock mailBox by GreenMail

I try to mock server with mailbox to test mail send by another apllication.
I try to use GreenMail. I want to my test received mail send from some apllication.
This is my code in test:` ServerSetup setup = new ServerSetup(3025, "localhost", "smtp");
GreenMail greenMail = new GreenMail(setup);
greenMail.setUser("user1#mail.com", "user1", "user1");
greenMail.start();
greenMail.waitForIncomingEmail(50000, 1);
Message[] messages = greenMail.getReceivedMessages();
try {
System.out.println(messages[0].getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
}`
I run this code and send mail from my personal mailbox to adress "user1#mail.com". And mail doesn't reaches. Sombody know what I do wrong ?Please for help. Maybe i should use another tool for what I want achieve ?

can't connect with pop3 server

i want to check my mails with an app. my code so far:
my provider uses SSL, see
here.
my code so far:
try {
//1) get the session object
Properties properties = new Properties();
properties.put("mail.pop3s.host", "pop.1und1.de");
properties.put("mail.pop3s.port", "995");
Session emailSession = Session.getDefaultInstance(properties);
//2) create the POP3 store object and connect with the pop server
POP3Store emailStore = (POP3Store) emailSession.getStore("pop3s");
emailStore.connect(<myMailAdress>, <password>);
//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(Message message:messages) {
String from = message.getFrom()[0].toString().toLowerCase();
Log.e("XXXXXX",from);
}
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
the code runs until emailStore.connect(). There i get the Exception
javax.mail.MessagingException: Connect failed; nested exception is: javax.net.ssl.SSLHandshakeException: Connection closed by peer.

how to send mail from glassfish server?

How to write a mail application with java mail API that run all time at glassfish server and search a specific time in the database and send mail at that time. i have web application written in JSF that need to send mail at specific time given in database.
For gmail, use below code
import org.apache.commons.mail.*;
public class GmailEmailWorking {
public static void main(String[] args) {
String myEmailId = "xyz#gmail.com";
String myPassword = "password";
String senderId = "xyz#yahoo.com";
try {
MultiPartEmail email = new MultiPartEmail();
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(myEmailId, myPassword));
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.setFrom(myEmailId);
email.setSubject("Hi");
email.setMsg("This is a test mail ... :-)\n\nPlease check attachements that I have sent.\n\nThanks,\nFahim");
email.addTo(senderId);
email.setTLS(true);
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("/Users/fahadparkar/Desktop/Fahim/tables.xlsx");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Excel");
attachment.setName("tables.xlsx");
email.attach(attachment);
email.send();
System.out.println("Mail sent!");
} catch (Exception e) {
System.out.println("Exception :: " + e);
}
}
}
Below are list of jar files you will need
commons-email-1.2.jar
activation.jar
mail.jar
To send from other server, you will need to do changes at below line
email.setSmtpPort(587);
email.setHostName("smtp.gmail.com");

Categories