2 slaks SLaks 于 2013.07.19 17:49 提问

使用 javamail 连接 SMTP 时的错误

这是我发送一个邮件使用的代码:

public String get_subject() {
        return _subject;
    }
    public void set_subject(String _subject) {
        this._subject = _subject;
    }
    public Mail() {                 
        _host = "mail.messagingengine.com";    
    _port = "587";  
        _sport = "587"; 
        _user = ""; // username
        _pass = ""; // password
        _from = ""; // email sent from
        _subject = ""; // email subject
        _body = ""; // email body
        _debuggable = false; // debug mode on or off - default off
        _auth = true; // smtp authentication - default on
        _multipart = new MimeMultipart();
        // There is something wrong with MailCap, javamail can not find a
        // handler for the multipart/mixed part, so this bit needs to be added.
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
        CommandMap.setDefaultCommandMap(mc);
    }
    public Mail(String user, String pass) {
        this();
        _user = user;
        _pass = pass;
    }
    public boolean send() throws Exception {
        Properties props = _setProperties();
        if (!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("")
                && !_body.equals("")) {
            Session session = Session.getInstance(props, this);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(_from));
            InternetAddress[] addressTo = new InternetAddress[_to.length];
            for (int i = 0; i < _to.length; i++) {
                addressTo[i] = new InternetAddress(_to[i]);
            }
            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
            msg.setSubject(_subject);
            msg.setSentDate(new Date());
            // setup message body
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(_body);
            _multipart.addBodyPart(messageBodyPart);
            // Put parts in message
            msg.setContent(_multipart);
            Transport.send(msg);
            return true;
        } else {
            return false;
        }
    }
    public void addAttachment(String filename) throws Exception {
        BodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);

        _multipart.addBodyPart(messageBodyPart);
    }
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(_user, _pass);
    }
    private Properties _setProperties() {
        Properties props = new Properties();

        props.put("mail.messagingengine.com", _host);
        // mail.messagingengine.com

        if (_debuggable) {
            props.put("mail.debug", "true");
        }
        if (_auth) {
            props.put("mail.smtp.auth", "true");
        }
        props.put("mail.smtp.port", _port);
        props.put("mail.smtp.socketFactory.port", _sport);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        return props;
    }
    // the getters and setters
    public String getBody() {
        return _body;
    }
    public void setBody(String _body) {
        this._body = _body;
    }
    // more of the getters and setters …..
}

在命令 transport.send(msg);后出现的错误:

javax.mail.MessagingException: Could not connect to SMTP host: mail.messagingengine.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x1f81518: Failure in SSL library, usually a protocol error
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:683 0x4025bc93:0x00000000)

1个回答

xiaoyan_12
xiaoyan_12   2013.07.22 13:23
已采纳

下面的代码希望能给你帮助:

public class Mail extends javax.mail.Authenticator { 
  private String _user; 
  private String _pass; 
  private String[] _to; 
  public String[] get_to() {
    return _to;
}
public void set_to(String[] _to) {
    this._to = _to;
}
public String get_from() {
    return _from;
}
public void set_from(String _from) {
    this._from = _from;
}
public String get_subject() {
    return _subject;
}
public void set_subject(String _subject) {
    this._subject = _subject;
}
private String _from; 
  private String _port; 
  private String _sport; 
  private String _host; 
  private String _subject; 
  private String _body; 
  private boolean _auth; 
  private boolean _debuggable; 
  private Multipart _multipart; 
  public Mail() { 
    _host = "mail.messagingengine.com"; // default smtp server 
    _port = "465"; // default smtp port 
    _sport = "465"; // default socketfactory port 
    _user = ""; // username 
    _pass = ""; // password 
    _from = ""; // email sent from 
    _subject = ""; // email subject 
    _body = ""; // email body 
    _debuggable = false; // debug mode on or off - default off 
    _auth = true; // smtp authentication - default on 
    _multipart = new MimeMultipart(); 
    // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
    CommandMap.setDefaultCommandMap(mc); 
  } 
  public Mail(String user, String pass) { 
    this(); 
    _user = user; 
    _pass = pass; 
  } 
  public boolean send() throws Exception { 
    Properties props = _setProperties(); 

    if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) { 
      Session session = Session.getInstance(props, this); 
      MimeMessage msg = new MimeMessage(session); 
      msg.setFrom(new InternetAddress(_from)); 
      InternetAddress[] addressTo = new InternetAddress[_to.length]; 
      for (int i = 0; i < _to.length; i++) { 
        addressTo[i] = new InternetAddress(_to[i]); 
      } 
      msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 
      msg.setSubject(_subject); 
      msg.setSentDate(new Date()); 
       // setup message body 
      BodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.setText(_body); 
      _multipart.addBodyPart(messageBodyPart); 
       // Put parts in message 
      msg.setContent(_multipart); 
      // send email 
      Transport.send(msg); 
       return true; 
    } else { 
      return false; 
    } 
  } 
  public void addAttachment(String filename) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    _multipart.addBodyPart(messageBodyPart); 
  } 
  @Override 
  public PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(_user, _pass); 
  } 
  private Properties _setProperties() { 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", _host); 
    if(_debuggable) { 
      props.put("mail.debug", "true"); 
    } 
    if(_auth) { 
      props.put("mail.smtp.auth", "true"); 
    } 
    props.put("mail.smtp.port", _port); 
    props.put("mail.smtp.socketFactory.port", _sport); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 
    return props; 
  } 
  // the getters and setters 
  public String getBody() { 
    return _body; 
  } 
   public void setBody(String _body) { 
    this._body = _body; 
  } 
  // more of the getters and setters ….. 
} 

我想错误是在:

_port = "465"; // default smtp port 
_sport = "465"; // default socketfactory port 

和这一行:

props.put("mail.messagingengine.com", _host);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
JavaMail学习笔记(一)、理解邮件传输协议(SMTP、POP3、IMAP、MIME)
电子邮件需要在邮件客户端和邮件服务器之间,以及两个邮件服务器之间进行传递,就必须遵循一定的规则,这些规则就是邮件传输协议。SMTP协议定了邮件客户端与SMTP服务之间,以及两台SMTP服务器之间发送邮件的通信规则;POP3/IMAP协议定义了邮件客户端与POP3服务器之间收发邮件的通信规则。 一、SMTP协议         SMTP(Simple Mail Transfer
JavaMail无法连接SMTP25端口问题
JavaMail无法连接SMTP25端口问题测试JavaMail发邮件功能,始终发生错误:javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25使用outlook收发邮件正常,使用telnet smtp.163.com 25不能连接什么错误?很是莫名其妙!从网
使用JavaMail连接带有TLS的SMTP
http://blog.sina.com.cn/s/blog_5595d514010005qj.html import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import ja
JavaMail使用SMTP协议发送电子邮件(详解)
JavaMail使用SMTP协议发送电子邮件(详解)
JavaMail使用SSL遇到安全证书问题
业务需求又发生的改变,登录模块中要用邮箱去验证,于是试试企业邮件服务器发邮件,结果使用SSL遇到 PKIX:nable to find valid certification path to requested target 通过搜索和查询资料发现CSDN上一篇文章给出了一个关键性的步骤-获取安全证书。 原文地址:  http://blog.csdn.net/faye0
JavaMail使用SMTP协议发送电子邮件
package cn.outofmemory.javamail; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import j
使用JavaMail进行简单SMTP邮件发送测试,从网易邮箱发送到QQ邮箱
本文使用的JavaMail版本为1.5.2 import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication;
JavaMail使用SSL的方式登录
[java] view plain copy print? public class JavaMailTest1 {      public static void main(String[] args) throws MessagingException {          Properties props = new Properties();          pro
JavaMail实现QQ邮箱发送报错问题的处理
最近在做一个与OA系统的邮箱接口,在测试代码的时候,分别用了126邮箱和QQ邮箱发送邮件,126邮箱可以成功发送,QQ就总是报错,虽然后来成功部署与OA邮箱接口,但是总想找到QQ邮箱发送不成功的原因,今天又在网上找资料,终于实现了QQ邮箱成功发送。 QQ邮箱服务器如下:                                    刚开始用25端口,怎么弄都是报错,网上好多帖子都说
java发送邮件(javaMail通过SMTP发送邮件)
java发送邮件一般使用在注册账号时、或其他通知信息时,网站会使用邮件定时发送、或触发发送邮件通知用户; 我是用的是maven开发,所以需要在pom文件中