dcsimg
JavaMail(Proxy Server)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   funky_pak
Posted On:   Saturday, May 10, 2003 12:17 AM

Hello everone !! Could anyone please let me know as to how should i make my java program compliant with proxy sever....i mean what i want to do is....i want to transfer/copy mails from any mail server into my hard-drive through java program(i am using JavaMail API). I am able to fetch mail from any Intranet's mail server which requires authentication but when i try to do that for any other email provider which gives free pop3 service (for example runbox.com,hotpop.com,softhome.net etc.)........UnknownHostException is thrown("connection failed"). In a nut shell ...i want my program to work through a proxy server.I mean while working under a proxy server i want to fetch mails from ANY mail server. I m attachi   More>>

Hello everone !! Could anyone please let me know as to how should i make my java program compliant with proxy sever....i mean what i want to do is....i want to transfer/copy mails from any mail server into my hard-drive through java program(i am using JavaMail API).

I am able to fetch mail from any Intranet's mail server which requires authentication but when i try to do that for any other email provider which gives free pop3 service (for example runbox.com,hotpop.com,softhome.net etc.)........UnknownHostException is thrown("connection failed").

In a nut shell ...i want my program to work through a proxy server.I mean while working under a proxy server i want to fetch mails from ANY mail server.

I m attaching my program with this mail for your kind reference.
Thank you.


			
import java.io.*;
import java.util.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.*;
import java.text.*;
public class FetchMail
{
public static void main (String args[]) throws Exception
{
function(args[0],args[1],args[2]);
}

public static void function(String args0,String args1,String args2) throws MessagingException, IOException,Exception
{
String host = args0;
String username = args1;
String password = args2;
Properties prop=System.getProperties();

prop.put("proxySet","true");
prop.put("proxyHost","proxy_host");
prop.put("proxyPort","3128");

java.net.Authenticator.setDefault(new MyAuthenticator());


Session session = Session.getInstance(prop,null );

Store store = session.getStore("pop3");
store.connect(host, username, password);

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));

Message message[] = folder.getMessages();


for (int n=message.length,i=n-1; i>=0; i--)
{
Date d=message[i].getSentDate();
String dat=d.toString();
String sub=dat.replace(':','-');


String from=((InternetAddress)message[i].getFrom()[0]).getPersonal();

if (from==null)
from=((InternetAddress)message[i].getFrom()[0]).getAddress();

String subject=message[i].getSubject();
String path;

path= "C:\Mails\"+from+"\";

String str = path + sub ;
File f=new File(path);
boolean b=f.mkdirs();


System.out.println("

Message "+ (i+1) + ": " + from + " " + subject);

System.out.println("Do you want to get the content?[Enter Y to go ahead <--- OR---> Q to end the session]");
String line = reader.readLine();
if (line.equalsIgnoreCase("Y"))
{
Object content = message[i].getContent();

if (content instanceof Multipart)
{
handleMultipart(str,(Multipart)content);
}
else
{
handlePart(str,message[i]);
}
}
else if (line.equalsIgnoreCase("Q"))
{
break;
}

}

folder.close(false);
store.close();
}

public static void handleMultipart(String str,Multipart multipart) throws MessagingException, IOException
{
for (int i=0, n=multipart.getCount(); i
{
handlePart(str,multipart.getBodyPart(i));
}
}

public static void handlePart(String str,Part part) throws MessagingException, IOException
{

String disposition = part.getDisposition();
String contentType = part.getContentType();

if (disposition == null)
{
System.out.println("Null: " + contentType);
int len=contentType.length();
String string=contentType.toLowerCase().substring(0, 10);
if ((len >= 10) && (string.equals("text/plain")) )
{
part.writeTo(System.out);

RandomAccessFile raf=new RandomAccessFile(str+".doc","rw");

InputStream is = part.getInputStream();

BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String thisLine=reader.readLine();

while (thisLine!=null)
{
raf.writeBytes(thisLine);
raf.writeByte(10);

thisLine=reader.readLine();
}

}
else
{
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
}
else if (disposition.equalsIgnoreCase(Part.ATTACHMENT))
{
System.out.println("Attachment: " + part.getFileName() +" : " + contentType);
saveFile(str,part.getFileName(), part.getInputStream());
}
else if (disposition.equalsIgnoreCase(Part.INLINE))
{
System.out.println("Inline: " + part.getFileName() + " : " + contentType);
saveFile(str,part.getFileName(), part.getInputStream());

}
else
{
System.out.println("Other: " + disposition);
}
}
public static void saveFile(String str,String filename,InputStream input) throws IOException
{

File file = new File(str+filename);
for (int i=0; file.exists(); i++)
{
file = new File(str+filename+i);
}


FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;

while ((aByte = bis.read()) != -1)
{
bos.write(aByte);
}

bos.flush();
bos.close();
bis.close();
}

}

class MyAuthenticator extends java.net.Authenticator{
protected java.net.PasswordAuthentication getPasswordAuthentication(){
return new java.net.PasswordAuthentication("username", "password".toCharArray());
}
}
   <<Less

Re: JavaMail(Proxy Server)

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, May 12, 2003 06:32 AM

As far as I know, typically firewalls and proxy servers explicitly prevent you from using the pop3 or imap protocols.

About | Sitemap | Contact