![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
EOF on Socket, exception..
Hello there,
i am just trying to implement a simple program that can connect to my gmail account and retrieve my e-mails, attachments etc. Here is my code: GetPic.java package IMAIL;
import javax.mail.*;
import java.util.*;
import java.io.*;
public class GetPic {
public static void main(String args[])
{
String username = "myusername@gmail.com";
String password = "mypassword";
String host = "pop.gmail.com";
int port=995;
// Create empty properties
Properties props = new Properties();
// Setup authentication, get session
Authenticator auth = new PopupAuthenticator();
Session session = Session.getDefaultInstance(props,auth);
try {
Store store = session.getStore("pop3");
store.connect(host,port, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// Get directory
Message[] message = folder.getMessages();
//Note: Trick for creating new image name. Erase it...
for (int i=0, n=message.length; i<n; i++) {
// Check out if it mime Message
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
//Now get attachment (multiparts)
Multipart mp = (Multipart)message[i].getContent();
int numberOfnodyparts = mp.getCount();
//Count multipart occurences
for (int j=0; j<numberOfnodyparts; j++) {
Part part = mp.getBodyPart(j);
//
String disposition = part.getDisposition();
//if it's attachment save it
if ((disposition != null) &&
((disposition.equals(Part.ATTACHMENT)))) {
//Print atatchment type
//System.out.println("Attachement type::" + part.getContentType());
//Save it only if attachment is an image
//NOTE: check for types: jpeg/png/gif
//NOTE: parse file's name and type and create a replica
System.out.println("Content-Type::\""+part.getContentType()+"END");
//savePic(part.getInputStream(), "C:\\aval\\inteliJ_Projects\\GetPicFromIMAIL\\"+i+".jpg");
}
}
}
folder.close(false);
store.close();
}
catch (Exception e)
{
System.out.println("oops something went wrong!::"+e);
}
}
private static void savePic(InputStream in, String s) throws IOException {
File pic = new File(s);
//NOTE: its better to use a buffer around the inputstream
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pic));
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}package IMAIL;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.swing.*;
import java.util.StringTokenizer;
public class PopupAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username, password;
String result = JOptionPane.showInputDialog("Enter 'username,password'");
StringTokenizer st = new StringTokenizer(result, ",");
username = st.nextToken();
password = st.nextToken();
return new PasswordAuthentication(username, password);
}
}I just want to save my attachments from my e-mail if there is an image. I have implemented the getExtension method also and is working properly with a Filechooser that i found on the net. There is not the whole program in this code, but only with this i get this type of error: oops something went wrong!::javax.mail.AuthenticationFailedException: EOF on socket Any ideas what to do? i searched all over the internet and didn't find something to help me. Thx in advance , sorry for the long post |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
Found it, thanks
|
|
|
|
|
|
#3 |
|
Programmer
|
What ended up being the problem? just curious.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
I had to set up manual the properties :
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
//Set manual Properties
props.setProperty( "mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty( "mail.pop3.socketFactory.fallback", "false");
props.setProperty( "mail.pop3.port", "995");
props.setProperty( "mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
// Setup authentication, get session
Authenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(props,auth);but i have to make a lot of things among this one |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|