aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail - why doesn Watch "JavaMail - why doesn New topic
Author

JavaMail - why doesn't this work???

Master Rice
Greenhorn

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. "mail.onetel.net.uk", "smtp.mail.onetel.net.uk", "mail.smtp.net.uk" but get the exceptions:
javax.mail.MessagingException: 501 Invalid domain name
or more frequently
javax.mail.MessagingException: Could not connect to the SMTP host: onetel.net.uk, 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 "mail.onetel.net.uk" ???
(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)?)
Cheers,
// NEIL
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 = "myAddress@onetel.net.uk";
String hostName = "mail.onetel.net.uk";
postMail(mailTo, sub, msg, mailFrom, hostName);
}
static public void postMail( String recipients[ ], String subject, String message , String from, String SMTPhost)
{
try{
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", 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);
msg.setFrom(addressFrom);
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.setSubject(subject);
msg.setContent(message, "text/plain");
// Send newMessage
Transport transport = session.getTransport("smtp");
transport.connect(SMTPhost, "myAddress@onetel.net.uk", "**myPassword**");
transport.sendMessage(msg, addressTo);
System.out.println("Message sent successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[This message has been edited by Master Rice (edited September 13, 2001).]
Raghavendra_Singh
Greenhorn

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 = "smtp.sharda.mahindrabt.com";
String from = "abc@xyz.com";
String to = "def@uvw.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", 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");
Transport.send(message);
System.out.println(" sent the message ");
System.out.println(" thanks ");
}
}
Willson Wong
Greenhorn

Joined: Jun 11, 2003
Posts: 3
I had a similar
Could not connect to the SMTP host: <myhost.company.com>, 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 <myhost.company.com> 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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JavaMail - why doesn't this work???