Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

JavaMail - why doesn't this work???  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ");
}
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!