我试图用javax发送eamil。我的代码如下:
private String emailSender(String emailTo, String emailFrom, String message, String subject, String password) {
    String status = "failed";
    try {
        String ccEmail = "";
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtps.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.smtps.auth", "true");
        props.put("mail.smtps.quitwait", "false");
        Session session = Session.getInstance(props, null);
        // -- Create a new message --
        final MimeMessage msg = new MimeMessage(session);
        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(emailFrom));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo, false));
        if (ccEmail.length() > 0) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
        }
        msg.setSubject(subject);
        msg.setText(message, "utf-8");
        msg.setSentDate(new Date());
        SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
        String host = StringUtils.substringAfter(emailFrom, "@");
        String emailName = StringUtils.substringBefore(emailFrom, "@");
        t.connect("smtp." + host, emailName, password);
        t.sendMessage(msg, msg.getAllRecipients());
        t.close();
        status = "Sent";
    } catch (Exception e) {
        LOGGER.error("error with sending email ", e);
    }
    return status;
}一般情况下,它工作正常。我可以通过gmail帐户或者雅虎发送..。但是,当我试图从contact@vayg.com帐户发送时,出现了这样的未知主机异常:
javax.mail.MessagingException: Unknown SMTP host: smtp.vayg.com;有什么解决办法吗?
发布于 2014-11-12 18:44:45
从管理员处获取smtp主机名和端口,然后在代码中使用它。
正如您在代码中提到的,gmail有自己的域和端口号。
在属性文件中添加适当的主机名和端口号,然后尝试运行代码。
https://stackoverflow.com/questions/26893960
复制相似问题