• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaMail and server port on Yahoo

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = "david@yahoo.com";
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 = "david@yahoo.com";
mail.message = "Hi. This is a Test!";
mail.subject = "Test";
mail.attach = new File("c:\\temp\\test.txt");
mail.sendMail();
System.out.println("END");
}
}
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this question to the Other APIs forum, where you stand a better chance of getting the best answer.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David and welcome to JavaRanch. This question should probably be posted in the Other Java APIs forum or possibly the Intermediate forum. I'm not totally sure about this but I don't believe that yahoo mail uses the SMTP protocol so you may not be able to use the code you posted. Most likely, port 25, which is normally used for SMTP mail, is blocked at mail.yahoo.com's firewall. To use your code, you will need to send the mail to an address with a known mail server, any Linux or Unix machine should have one set up on it.
 
david solomon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
Many thanks for your swift reply. I do not have access to any other server, can you suggest a free Email server download I can use for testing?. I have already installed TomCat server and it is functioning perfectly all right, is it possible to use it for Email?. If yes then it will be great if you have an example which will work on TomCat.
Best of Regards
David
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What OS are you using? I'm running Windows 2000 Pro and it has a mail server installed. If you are running Windows, open a command console and type:


If you see something like:

then you have an SMTP (mail) server running on the localhost.
If you see:

then you will need to install a mail server on your machine.
Once you have an SMTP server, change your code from:

to:
 
david solomon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
I initiated the telnet session on my pc and you are correct, I do not have an Email server; but when I tried to connect to the ntl server I got a similar output to your server telling me the ntl server is ready. I changed the code to specify ntl instead of yahoo as in:
private final String MAIL_HOST="mail.ntlworld.com";
and also to :
String to=req.getParameter("david@ntlworld.com");

And when I tried to run the application to send myself an email to my ntl account, I start to get the following error message:
java.lang.ClassCastException
at sun.applet.AppletPanel.createApplet(AppletPanel.java:567)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:496)
at sun.applet.AppletPanel.run(AppletPanel.java:293)
at java.lang.Thread.run(Thread.java:536)
any idea what is needed to be done? I am very grateful for your input on this.
Jason,
Many thanks for moving the question to this forum. I was thinking about the possibility of making use of Microsoft �Outlook Express� as a means of sending/receiving emails. This utility is available and can be used to receive/send emails without the need of installing an Email server. I searched for a java call/class to this utility so to send an email via a batch processes(not interactive); but I could not locate one. If there is such a class then please advice; but if there is not then can you take this further to a relevant java forum/group whom might be able to provide the java community with such a class.
Cheers
David
 
reply
    Bookmark Topic Watch Topic
  • New Topic