try{
//Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory ";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty( "mail.smtp.host ", "smtp.gmail.com ");
props.setProperty( "mail.smtp.socketFactory.class ", SSL_FACTORY);
props.setProperty( "mail.smtp.socketFactory.fallback ", "false ");
props.setProperty( "mail.smtp.port ", "465 ");
props.setProperty( "mail.smtp.socketFactory.port ", "465 ");
props.setProperty( "mail.smtp.auth ", "true ");
props.setProperty( "mail.smtp.ssl ", "true ");
//props.put( "mail.smtp.user ", "hombreelk ");
//props.put( "mail.smtp.password ", "hombre123 ");
final String username = "wahaha1982 ";
final String password = "wahaha123 ";
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@gmail.com "));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse( "hjh1982@gmail.com ",false));
msg.setSubject( "Hello ");
msg.setText( "How are you ");
msg.setSentDate(new java.util.Date());
Transport.send(msg);
System.out.println( "Message sent. ");
}catch(Exception ex)
{
ex.printStackTrace();
}
用这段程序可以将邮件发送出去,但是报下面的异常:
javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unsupported record version Unknown-50.49
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1598)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:685)
at javax.mail.Transport.send0(Transport.java:171)
at javax.mail.Transport.send(Transport.java:98)
at cn.handinfo.mail.impl.MailServiceImpl.main(MailServiceImpl.java:473)
Caused by: javax.net.ssl.SSLException: Unsupported record version Unknown-50.49
at com.sun.net.ssl.internal.ssl.InputRecord.readV3Record(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)