简体   繁体   English

javax未知主机异常?

[英]javax unknown host exception?

Im trying to send eamil using javax. 我正在尝试使用javax发送eamil。 My code is below: 我的代码如下:

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;
}

Generally it works properly. 通常它可以正常工作。 I can send like via gmail account or yahoo ... but when i'm trying to send from contact@vayg.com account, got unknown host exception like this: 我可以通过gmail帐户或yahoo进行发送...但是当我尝试从contact@vayg.com帐户进行发送时,出现未知的主机异常,如下所示:

javax.mail.MessagingException: Unknown SMTP host: smtp.vayg.com;

Any solutions? 有什么办法吗?

You're assuming the host has a 3rd level domain and always prefix it with "smtp." 您假设主机具有第3级域,并且始终以“ smtp”作为前缀。

But that might not always be the case. 但这并非总是如此。 The smtp host name could be anything. smtp主机名可以是任何名称。

Get your smtp host name and the port from your admin and then use it in your code. 从管理员那里获取您的smtp主机名和端口,然后在代码中使用它。

Eg: as you mentioned in your code, gmail has its own domain and port numbers. 例如:如您在代码中提到的,gmail有其自己的域和端口号。

add the appropriate host name and port number in your properties file and then try running your code. 在属性文件中添加适当的主机名和端口号,然后尝试运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM