首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >javax未知主机异常?

javax未知主机异常?
EN

Stack Overflow用户
提问于 2014-11-12 18:29:25
回答 2查看 148关注 0票数 0

我试图用javax发送eamil。我的代码如下:

代码语言:javascript
运行
复制
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帐户发送时,出现了这样的未知主机异常:

代码语言:javascript
运行
复制
javax.mail.MessagingException: Unknown SMTP host: smtp.vayg.com;

有什么解决办法吗?

EN

Stack Overflow用户

回答已采纳

发布于 2014-11-12 18:44:45

从管理员处获取smtp主机名和端口,然后在代码中使用它。

正如您在代码中提到的,gmail有自己的域和端口号。

在属性文件中添加适当的主机名和端口号,然后尝试运行代码。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26893960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档