使用java使用godaddy帐户发送电子邮件

yptwkmov  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(854)

我正试图用我的 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帐户时,会抛出异常。
请指导我如何解决这个问题。。。
提前谢谢。。。

lf5gs5x2

lf5gs5x21#

我在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"); 在发送邮件之前(否则它使用的是我的系统名,并给出了一个错误),此设置工作正常。

uqjltbpv

uqjltbpv2#

无身份验证信任证书的javamail ssl

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

需要javax.mail 1.5.2及更高版本

rseugnpd

rseugnpd3#

下面是一个对我来说绝对有效的完整方法(我还在邮件中使用了附件):

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

相关问题