使用Java发送Godaddy账号的电子邮件

3
我正在尝试使用我的godaddy帐户在Java中发送电子邮件。以下是我的代码。
Properties props = System.getProperties();
props.put("mail.transport.protocol","smtp" );
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.smtp.host","smtpout.secureserver.net");

props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
props.put("mail.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");


 Authenticator auth = new SMTPAuthenticator();
 Session session=Session.getInstance(props,auth);
 session.setDebug(true);

 // -- Create a new message --
 Transport transport=session.getTransport();

 Message msg = new MimeMessage(session);
 // -- Set the FROM and TO fields --
 msg.setFrom(new InternetAddress(""email@domain.com));
 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("email@domain.com", false));
 msg.setSubject("subject");
 msg.setText("Message");

 transport.connect();
 Transport.send(msg);
 transport.close();

执行时遇到以下异常:

com.sun.mail.util.MailConnectException: 无法连接到主机和端口:smtpout.secureserver.net,465; 超时-1; 嵌套异常为: java.net.UnknownHostException: smtpout.secureserver.net

PS:当我使用Gmail帐户进行身份验证时,它正常工作并成功发送电子邮件。 当我使用Godaddy帐户时,会抛出异常。
请指导我如何解决此问题...
提前致谢...

您确定SMTP服务器的主机名正确吗? - Stefan
我已经删除了我的回答,因为我没有Godaddy账户,所以无法在我的端上检查。只需阅读此文章http://www.javaquery.com/2011/09/how-to-send-mail-from-godaddy-java-mail.html。 - SpringLearner
@SpringLearner 我尝试了你提供链接中相同的方法,但它也抛出相同的异常。 - manoj
@Stefan,我确定我遵循了SpringLearner提供的链接中给出的相同流程。 - manoj
3个回答

4

我的SpringBoot配置(将domainname.com替换为您的域名)

spring:
  mail:
    host: smtpout.secureserver.net
    port: 465
    username: info@domainname.com
    password: password
    protocol: smtp
    properties.mail.smtp:
      auth: true
      ssl.enable: true
      ssl.trust: "*"

还有一点,我必须在发送邮件之前添加mimeMessageHelper.setFrom("info@domainname.com");(否则它会采用我的系统名称并出现错误),这个设置起作用了。


1
这些信息非常有用,对我很有效,谢谢Krishna Kumar,特别是那一小段代码mimeMessageHelper.setFrom("info@domainname.com");。 - melwinpintoe

1
这是一个完整的方法,对于我来说运作良好(我还在消息中使用了附件):
private void sendUsingSmtp(MailDetail mailDetail) {
        Properties props = new Properties();
        props.put("mail.host", "smtpout.secureserver.net");
        props.put("mail.port", "465");
        props.put("mail.username", "info@domainName.com");
        props.put("mail.password", “password”);
        props.put("mail.protocol", "smtp");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.trust", "*");


        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("info@domainName”, “password");
            }
        });
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress("info@domainName.com", false);
            msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(“targetEmail@gmail.com"));
            msg.setSubject("Test Subject.");
            msg.setContent("Test Content.", "text/html");
            msg.setSentDate(new Date());

            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent("Test Content.", "text/html");

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            MimeBodyPart attachPart = new MimeBodyPart();

            attachPart.attachFile("/var/tmp/abc.txt");
            multipart.addBodyPart(attachPart);
            msg.setContent(multipart);
            Transport.send(msg);
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接