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);
}
}

