Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java mail problem : could not connect to smtp host .. reponse:-1

843830Nov 2 2005 — edited Nov 9 2005
Below is the code and output respectively. Please help.


import java.util.*;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* Class EmailHelper
*
* An email helper:
*
* Hashtable mail = new Hashtable();
* mail.put("from", "dion@almaer.com");
* mail.put("to", "dion@almaer.com");
* mail.put("subject", "a subject");
* mail.put("body", "the body");
*
* EmailHelper.sendmail(mail);
*
* Currently only supports one email in to address
*/
public class EmailHelper {

private static boolean DEBUG = true;
private static Hashtable DEFAULT_SETTINGS = new Hashtable();
static {
DEFAULT_SETTINGS.put("mail.transport.protocol", "smtp");
DEFAULT_SETTINGS.put("mail.smtp.host", "172.18.18.104");
DEFAULT_SETTINGS.put("from", "unknown@unknown.com");
DEFAULT_SETTINGS.put("body", "no body");
DEFAULT_SETTINGS.put("subject", "no subject");
}

/* Only static methods here chaps */
private EmailHelper() {}


/**
* Static Method: sendmail(Map mail)
*
* @param mail A map of the settings
*/
public static void sendmail(Map mail) throws Exception {

Properties props = new Properties();
props.put( "mail.transport.protocol", EmailHelper.returnDefault(mail, "mail.transport.protocol") );
props.put( "mail.smtp.host", EmailHelper.returnDefault(mail, "mail.smtp.host") );

Session session = Session.getDefaultInstance(props, null);

/* DEBUG: */
if (DEBUG) {
session.setDebug(true);
Properties p = session.getProperties();
Enumeration e = p.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println("Key: " + key);
System.out.println("Value: " + p.getProperty( key ));
}
}

// Construct a MimeMessage
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress( EmailHelper.returnDefault(mail,"from") ) );
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EmailHelper.returnDefault(mail,"to"), false));

// -- set a CC: or BCC:
String cc = (String) mail.get("cc");
String bcc = (String) mail.get("bcc");
if (cc != null && (!cc.trim().equals(""))) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
if (bcc != null && (!bcc.trim().equals(""))) msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));

msg.setSubject(EmailHelper.returnDefault(mail,"subject"));
msg.setSentDate(new Date());
msg.setText(EmailHelper.returnDefault(mail,"body"));

// Send the message.
Transport.send(msg);
}


/**
* Static Method: returnDefault(map, key)
*
* @param map Map
* @param value String
* @param default String
*
* @return the value, or the default value if the value is null
*/
public static String returnDefault(Map map, String key) {
String value = (String) map.get(key);
String realValue = (String) ( (value == null) ? DEFAULT_SETTINGS.get(key) : value );
return realValue;
}


/**
* Static Method: Command Line: java com.customware.util.EmailHelper to_addr from_addr subject body
*
* @param args String []
*/
public static void main(String args[]) throws Exception {
if (args.length == 0) {
System.out.println("Usage: EmailHelper to_addr [from_addr] [subject] [body]");
System.exit(-1);
}

Hashtable mail = new Hashtable();
mail.put("to", args[0]);
if (args.length > 1) mail.put("from", args[1]);
if (args.length > 2) mail.put("subject", args[2]);
if (args.length > 3) mail.put("body", args[3]);

EmailHelper.sendmail(mail);
}
}


Output:

DEBUG: setDebug: JavaMail version 1.3.1
Key: mail.transport.protocol
Value: smtp
Key: mail.smtp.host
Value: 172.18.18.104
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "172.18.18.104", port 25

DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: could not connect to host "172.18.18.104", port: 25, response: -1

Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: 172
.18.18.104, port: 25, response: -1
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at EmailHelper.sendmail(EmailHelper.java:81)
at EmailHelper.main(EmailHelper.java:118)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2005
Added on Nov 2 2005
10 comments
1,534 views