permaculture playing cards
The moose likes Other JSE/JEE APIs and the fly likes JavaMail - why doesn't this work??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail - why doesn Watch "JavaMail - why doesn New topic

JavaMail - why doesn't this work???

Master Rice

Joined: Aug 05, 2001
Posts: 8
Hi all,
I'm trying to learn how to write a mail sending application. Got the following code, but it doesn't work.
The problem lies in the transport.connect() bit (I believe). I have tried numerous entries for the mail server input:
e.g. "", "", "" but get the exceptions:
javax.mail.MessagingException: 501 Invalid domain name
or more frequently
javax.mail.MessagingException: Could not connect to the SMTP host:, port: 25;
I am making a mistake somewhere, can someone spot it? I would be eternally at your service if you could help me... In MSOffice, my outgoing mail (SMTP) is "" ???
(Just read previous forum entries on this.. Do I need to install an smtp server on my PC? Can I not use my ISP to send mail through java? If so, where can I get a smtp server (for free)?)
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class mailSender
public static void main(String[] args)
String[] mailTo = {"someone@someaddress"};
String sub = "javax mail";
String msg = "Hello Me";
String mailFrom = "";
String hostName = "";
postMail(mailTo, sub, msg, mailFrom, hostName);
static public void postMail( String recipients[ ], String subject, String message , String from, String SMTPhost)
//Set the host smtp address
Properties props = new Properties();
props.put("", SMTPhost);
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++){
addressTo[i] = new InternetAddress(recipients[i]);
// Setting the Subject and Content Type
msg.setContent(message, "text/plain");
// Send newMessage
Transport transport = session.getTransport("smtp");
transport.connect(SMTPhost, "", "**myPassword**");
transport.sendMessage(msg, addressTo);
System.out.println("Message sent successfully");
catch(Exception e)
[This message has been edited by Master Rice (edited September 13, 2001).]

Joined: Sep 20, 2001
Posts: 2
Hi Rice,
Try this one
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail
public static void main (String args[]) throws Exception
String host = "";
String from = "";
String to = "";
Properties props = System.getProperties();
props.put("", host);
Session session = Session.getDefaultInstance(props,null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
System.out.println(" setting subject and message ");
message.setSubject("hi ");
message.setText("how r u");
System.out.println(" sent the message ");
System.out.println(" thanks ");
Willson Wong

Joined: Jun 11, 2003
Posts: 3
I had a similar
Could not connect to the SMTP host: <>, port: 25
problem except that I couldn't figure why it worked 2 months ago with my jsp script.
I tried and it was ok. Then I tried and I could not connect.
Then I checked /var/log/mail/warnings and found a
After editing the /var/spool/postfix/etc/hosts file to match and doing a reboot, the system then worked again and was about to send out javamail.
I think what happened was that the /var/spool/postfix/etc/hosts file was not allowing connections to be made to <> on port 25. This is not obvious, as I had scanned (with nmap) port 25 on localhost and everything had seemed fine.
So sometimes, the fault is not in the jsp script but the server setup.
Hope this helps.
Have you checked out Aspose?
subject: JavaMail - why doesn't this work???
It's not a secret anymore!