簡體   English   中英

使用java使用godaddy帳戶發送電子郵件

[英]Sending email using godaddy account using java

我正在嘗試使用我的 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 帳戶時,會拋出異常。

請指導我如何解決這個問題...

提前致謝...

我在 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"); 在發送郵件之前(否則它會使用我的系統名稱並給出錯誤)並且此設置有效。

這是對我來說絕對有效的完整方法(我還在郵件中使用了附件):

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

無論如何都沒有身份驗證信任證書的JavaMail SSL

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

需要 javax.mail 1.5.2 及更高版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM