A real mind boggler

I have been trying out the following code. The problem is when i have logged into my Internet account which also provides me with my email service the code works fine. No problems. But when i surf on a different account to my email service provider the code fails. This is the message it returns

Exception in thread "main" javax.mail.SendFailedException: Sending failed;

nested exception is:

class javax.mail.MessagingException: Could not connect to SMTP host: mai

l.btopenworld.com, port: 25;

nested exception is:

java.net.ConnectException: Operation timed out: connect

at javax.mail.Transport.send0(Transport.java:218)

at javax.mail.Transport.send(Transport.java:80)

at PostMail.postMail(PostMail.java:49)

at PostMail.main(PostMail.java:11)

This is my code

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

public class SendMail {

public SendMail() {

}

public static void main(String[] args) throws Exception{

send("myuser", "mypass", "","JavaMail2", "Hello");

}

public static void send(String userName, String password,String toAddr, String subject, String content) throws Exception{

String hostname = "mail.btopenworld.com";

Properties p = System.getProperties();

p.put("mail.smtp.host", hostname);

p.put("mail.smtp.auth","true");

NCSAuthenticator myau = new NCSAuthenticator(userName, password);

Session se = Session.getInstance(p,myau);

se.setDebug(true);

try {

InternetAddress iaFrom = new InternetAddress("sentfromemail");

InternetAddress iaTo = new InternetAddress("senttoemail");

MimeMessage msg = new MimeMessage(se);

msg.setSubject(subject);

msg.setText(content,"GB2312");

msg.setSentDate(new Date());

msg.setFrom(iaFrom);

msg.setRecipient(Message.RecipientType.TO,iaTo);

Transport transport = se.getTransport("smtp");

transport.connect

(hostname, userName, password);

msg.saveChanges();

transport.sendMessage(msg, msg.getAllRecipients());

transport.close();

}

catch(AuthenticationFailedException afe){

//System.out.println("AuthenticationFailedException Open!!!");

afe.printStackTrace() ;

}

catch(javax.mail.SendFailedException sfe){

//System.out.println("SendFailedException Open!!!");

sfe.printStackTrace();

}

catch(Exception e){

//System.out.println("General error!");

e.printStackTrace();

}

}

}

class NCSAuthenticator extends javax.mail.Authenticator{

private String userName = null;

private String password = null;

public NCSAuthenticator(String userName, String password) {

this.userName = userName;

this.password = password;

}

public PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(userName, password);

}

}

[3032 byte] By [mcai8aa2] at [2007-9-27 18:47:11]
# 1
dear friend, your code looks absolutely fine. yu need to chec your firewall settings if ur behind a firewall or u can try using IP address of your mail host.try and tell me what u got bcoz this code appears fine.regards,suraj
sberwal at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi this will help u

just try running this application ..

if it connects to u r mail server port 25 or not than let me know..

First write mail server name if it dosents work

than mail server IP

import java.io.*;

import java.net.*;

import java.util.*;

public class test {

public static void main(String[] args) {

new test(args);

}

public test(String[] args) {

try {

System.out.println("test");

Socket aTestSocket = new Socket("WRITE MAILServer NAME OR IP", 25);

System.out.println("connection established.");

} catch (Exception e) {

e.printStackTrace();

}

}

}

java_proind at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Thanks for replying back I ran the program you requested and got the following errorjava.net.UnknownHostException: WRITE MAILServer NAME OR IP.
mcai8aa2 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I am not running any firewall software or anything else. But the thing is when surfing using the same company that hosts my email it works fine. For example my mail is being hosted by BTOpenworld.com. When i surf using my BTOpenworld account their is not problems. But when i surf using AOL and try to access my BTOpenworld emails it fails. Accessing Yahoo it works fine

mcai8aa2 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Hi that means u r not able to connect to u r mail server any way ..pls check exactly that is mailserver name fine and even IP ..as it shd work let me know with code
java_proind at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

The mailserver name is correct because my program works fine when i am logged into the internet using the same username/password as the mailserver. I have a feeling that they might have some sort of security feature stopping this from happening. I used the ping feature to connect to the host and it timed out. Which means it must be a firewall issue on the mailserver side. Also i have written another program which reads and send emails using Yahoo and this works fine through both accounts.

mcai8aa2 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

1) BT Open world is not given permission to access its mail server for out siders like AOL etc etc.

2) Or U have to set something in your BTOpen World account just like we do in yahoo mail options.

3)Or there May be IP address Or Port no error.

I think the first one is right.

Bye

jini1357 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
I actually didnt think that i would need to make some setting changes inorder to make it work. That might be why it doesnt work. I will look into this. Thanks for replying this gives me some sort of idea of what to do next
mcai8aa2 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9
Hi u can use sun.net API to mail..or u have to get the port open 25 for u r smtp server ..just tell me if any problem in using sun.net regds
java_proind at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

Thanks for replying i have managed to resolve the issue i contacted my mailserver provider and thay have told me they have security features which stop unauthorised accesses which means when you have to be surfing from their account otherwise it will not work

Thanks for the help anyway i plan to use Yahoo as i can send and read emails from their.

mcai8aa2 at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11

Hello mcai8aa2,

I am new to java mail. I found ok to send mail using my company and yahoo account. But when I tried to read mails using them,different things happen. For my Yahoo Id it says connection failure due to time out. As you have succeeded to read mails using Yahoo ID, would u please help me to send the codes. My mailing address is bibagimon@yahoo.com. Thanks in advance.

jdreamer at 2007-7-6 20:06:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...