Hi all,
Picked a code on JavaMail, I ran it then it complained about the server port(25). My email account is on �Yahoo� and my provider is �NTL� and I managed to find the port set on my pc to be �110�. Any suggestions to correct the code?. Do I need to specify the port and if so then how?.
cheers
--------------------------------------------- error message -------------
java.lang.Exception: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: mail.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at TestMail.sendMail(TestMail.java:76)
at TestMail.main(TestMail.java:87)
Exception in
thread "main"
--------------- the tested code -------------
import javax.mail.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
import java.applet.*;
import java.awt.*;
import java.io.File;
import java.util.Properties;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class TestMail
{
/* Bean Properties */
private
String to = "
[email protected]";
private String from = null;
private String subject = null;
private String message = null;
private File attach =null;
public static Properties props = null;
public static Session session = null;
static {
/* Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "mail.yahoo.com");
session = Session.getDefaultInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}
public void setFrom(String from) {
this.from = from;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setMessage(String message) {
this.message = message;
}
public void setAttach(File attach) {
this.attach = attach;
}
/* Sends Email */
public void sendMail() throws Exception {
/*if(!this.everythingIsSet())
throw new Exception("Could not send email.");*/
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(this.message);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
javax.activation.DataSource fds = new FileDataSource(attach);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
message.setContent(mp);
//message.setText(this.message);
Transport.send(message);
System.out.println("Message sent!");
}
catch (MessagingException e) {
throw new Exception(e.getMessage());
}
}
public static void main(String args[]) throws Exception
{
System.out.println("START");
TestMail mail = new TestMail();
mail.from = "
[email protected]";
mail.message = "Hi. This is a
Test!";
mail.subject = "Test";
mail.attach = new File("c:\\temp\\test.txt");
mail.sendMail();
System.out.println("END");
}
}