通过Java Mail API发送电子邮件时出错?

时间:2015-07-21 09:57:39

标签: java javamail

尝试通过Java Mail API发送电子邮件时出现以下错误?这个错误意味着什么?

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)

这是我的代码,我设置了所有参数(从,到,主题和附件)

public static void send(MailUtil mailUtil) throws MessagingException {
            MimeMessage message = new MimeMessage(session);
            if (props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER) != null) {              
                message.setFrom(new InternetAddress(props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER)));
            } else {
                message.setFrom(new InternetAddress(mailUtil.getFrom()));
            }
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(mailUtil.getTo()));
            if (mailUtil.getBcc() != null && mailUtil.getBcc().trim().length() > 0) {
                message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailUtil.getBcc()));
            } else {
                message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(""));
            }

            message.setSubject(mailUtil.getSubject(), "UTF-8");

            // Check for files list and attach them.
            if (mailUtil.attachmentFiles != null && mailUtil.attachmentFiles.size() > 0) {
                Multipart multipart = new MimeMultipart();

                // Set content.
                BodyPart messageBodyPart =new MimeBodyPart();
                messageBodyPart.setContent(mailUtil.getContent(), "text/plain; charset=utf-8");
                multipart.addBodyPart(messageBodyPart);

                // Attach files.
                for (File file : mailUtil.attachmentFiles) {
                    messageBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(file);
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    messageBodyPart.setFileName(file.getName());
                    multipart.addBodyPart(messageBodyPart);
                }

                message.setContent(multipart);
            } else {
                //message.setContent("<h1>Hello world</h1>", "text/html");
                message.setContent(mailUtil.getContent(), "text/html; charset=UTF-8");
            }           
            Transport.send(message);
    }

我只是觉得我的参数有问题吗?

Belows是我的配置

mail.smtp.port=465
mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.timeout=25000

mail.smtp.host=smtp.gmail.com
mail.username = username@gmail.com
mail.password = mypassword
mail.sender = sender@gmail.com
mail.receiver = receiver@gmail.com
mail.subject = mysubject

我正在使用谷歌邮件服务器!我不认为那里有问题!

Belows是会话启动

final String userName = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_USERNAME);
final String passWord = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_PASSWORD);

session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName,
                                passWord);
                    }
            });

3 个答案:

答案 0 :(得分:7)

我认为它绝对与服务器配置有关。 当我将端口配置从465更改为587时,它解决了我的问题! 无论如何,谢谢你的帮助!

答案 1 :(得分:0)

我不确定为什么你使用“邮件”对象来设置连接属性,而是尝试这个,它应该可以工作,我已经测试过了自己:

private Properties props;
props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587"); 

session = Session.getInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });
try {
        message.setFrom(new InternetAddress("YOUR EMAIL ADDRESS HERE"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECEIVER EMAIL ADDRESS HERE"));
        message.setSubject("SUBJECT");

        message.setText("THE EMAIL TEXT");
        Transport.send(message);
    } catch (MessagingException e) {e.printStackTrace();}
}

答案 2 :(得分:0)

您的代码和配置包含许多common mistakes。特别是Session.getDefaultInstance may mean you're not using the configuration you think you're using的使用。如果修复无法解决您的问题,请发布JavaMail session debug output