Hi,
I am trying to configure JavaMail to send an email via SMTP over SSL, and I keep getting the following error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.postoffice.n
et, port: 25;
nested exception is:
java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFact
ory" socket factory to host, port: smtp.postoffice.net, 465; Exception: java.lan
g.reflect.InvocationTargetException
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:689)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:81
)
at javax.mail.Service.connect(Service.java:120)
at javax.mail.Service.connect(Service.java:41)
at msgsend.<init>(msgsend.java:176)
at msgsend.main(msgsend.java:53)
As one can see from the exception, my smtp host is smtp.postoffice.net. If someone out there has managed to send email with JavaMail via this server, any help would be greatly appreciated. FYI, I can configure Netscape 6.2 to send mail with the same smtp hostname, username and password, without having to specify any other information like port number.
I have setup SMTP over SSL with the following parameters:
props.put("mail.smtp.host", "smtp.postoffice.net");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "<username>");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.socketFactory.port", "465");
// Get a Session object
Session session = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<username>", "<password>");
}
});
The username & password (which I have hidden for obvious reasons) are not an issue as I am certain they are correct. The one thing I have not done that was described in a javaworld article on JavaMail & SSL is setting up certificates and TrustManagers from my code, but I am guessing that the above error occurs during socket creation much before the security checks are done. Please correct me If I am wrong.
I have also attached the java code that I am using to test the functionality.
regards,
Vished Kumar
I am trying to configure JavaMail to send an email via SMTP over SSL, and I keep getting the following error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.postoffice.n
et, port: 25;
nested exception is:
java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFact
ory" socket factory to host, port: smtp.postoffice.net, 465; Exception: java.lan
g.reflect.InvocationTargetException
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:689)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:81
)
at javax.mail.Service.connect(Service.java:120)
at javax.mail.Service.connect(Service.java:41)
at msgsend.<init>(msgsend.java:176)
at msgsend.main(msgsend.java:53)
As one can see from the exception, my smtp host is smtp.postoffice.net. If someone out there has managed to send email with JavaMail via this server, any help would be greatly appreciated. FYI, I can configure Netscape 6.2 to send mail with the same smtp hostname, username and password, without having to specify any other information like port number.
I have setup SMTP over SSL with the following parameters:
props.put("mail.smtp.host", "smtp.postoffice.net");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "<username>");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.socketFactory.port", "465");
// Get a Session object
Session session = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<username>", "<password>");
}
});
The username & password (which I have hidden for obvious reasons) are not an issue as I am certain they are correct. The one thing I have not done that was described in a javaworld article on JavaMail & SSL is setting up certificates and TrustManagers from my code, but I am guessing that the above error occurs during socket creation much before the security checks are done. Please correct me If I am wrong.
I have also attached the java code that I am using to test the functionality.
regards,
Vished Kumar
/*
* @(#)msgsend.java 1.15 00/05/24
*
* Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Demo app that shows how to construct and send an RFC822
* (singlepart) message.
*
* XXX - allow more than one recipient on the command line
*
* @author Max Spivak
* @author Bill Shannon
*/
public class msgsend {
public static void main(String[] argv) {
System.out.println("java.home="+System.getProperty("java.home"));
new msgsend(argv);
}
public msgsend(String[] argv) {
String to, subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind;
for (optind = 0; optind < argv.length; optind++) {
if (argv[optind].equals("-T")) {
protocol = argv[++optind];
} else if (argv[optind].equals("-H")) {
host = argv[++optind];
} else if (argv[optind].equals("-U")) {
user = argv[++optind];
} else if (argv[optind].equals("-P")) {
password = argv[++optind];
} else if (argv[optind].equals("-M")) {
mailhost = argv[++optind];
} else if (argv[optind].equals("-f")) {
record = argv[++optind];
} else if (argv[optind].equals("-s")) {
subject = argv[++optind];
} else if (argv[optind].equals("-o")) { // originator
from = argv[++optind];
} else if (argv[optind].equals("-c")) {
cc = argv[++optind];
} else if (argv[optind].equals("-b")) {
bcc = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-d")) {
debug = true;
} else if (argv[optind].equals("--")) {
optind++;
break;
} else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
System.out.println(
"\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
System.out.println(
"\t[-f record-mailbox] [-M transport-host] [-d] [address]");
System.exit(1);
} else {
break;
}
}
try {
if (optind < argv.length) {
// XXX - concatenate all remaining arguments
to = argv[optind];
System.out.println("To: " + to);
} else {
System.out.print("To: ");
System.out.flush();
to = in.readLine();
}
if (subject == null) {
System.out.print("Subject: ");
System.out.flush();
subject = in.readLine();
} else {
System.out.println("Subject: " + subject);
}
Properties props = System.getProperties();
// XXX - could use Session.getTransport() and Transport.connect()
// XXX - assume we're using SMTP
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "<username>");
java.security.Security.addProvider(new
com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.socketFactory.port", "465"); // Well-known port
of SMTP over SSL
// Get a Session object
Session session = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<username>", "<password>");
}
});
if (debug)
session.setDebug(true);
// construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
collect(in, msg);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
// send the thing off
Transport tr = session.getTransport("smtp");
tr.connect(mailhost, "<username>", "<password>");
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
System.out.println("\nMail was sent successfully.");
// Keep a copy, if requested.
if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null)
store = session.getStore(protocol);
else
store = session.getStore();
// Connect
if (host != null || user != null || password != null)
store.connect(host, user, password);
else
store.connect();
}
// Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
System.err.println("Can't get record folder.");
System.exit(1);
}
if (!folder.exists())
folder.create(Folder.HOLDS_MESSAGES);
Message[] msgs = new Message[1];
msgs[0] = msg;
folder.appendMessages(msgs);
System.out.println("Mail was recorded successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void collect(BufferedReader in, Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(sb.toString());
}
}
To change your membership options, refer to: http://www.sys-con.com/java/list.cfm
