【问题标题】:Sending email using godaddy account using java使用java使用godaddy帐户发送电子邮件
【发布时间】:2020-05-19 23:20:33
【问题描述】:

我正在尝试使用我在 java 中的 godaddy 帐户发送电子邮件。下面是我的代码。

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 服务器的主机名正确吗?
  • 我已经删除了我的答案,我没有godaddy账号所以不能在我身边查看。请看这篇文章javaquery.com/2011/09/…
  • @SpringLearner 我从您提供的链接中尝试了相同的方法,但它也抛出了相同的异常。
  • @Stefan ya 我确信我遵循 SpringLearner 提供的链接中给出的相同过程......

标签: java email


【解决方案1】:

我在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");(否则它会占用我的系统名称并给出错误)并且此设置有效。

【讨论】:

  • 这个信息非常有用,对我有用,谢谢 Krishna Kumar,尤其是一小段代码 mimeMessageHelper.setFrom("info@domainname.com");工作。
【解决方案2】:

这是对我来说绝对没问题的完整方法(我还在消息中使用了附件):

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

【讨论】:

    【解决方案3】:

    JavaMail SSL with no Authentication trust certificate regardless

        MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
        socketFactory.setTrustAllHosts(true);
    

    需要 javax.mail 1.5.2 及更高版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 2012-03-30
      • 2021-05-06
      • 2016-04-20
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多