简体   繁体   中英

Sending email using godaddy account using java

I am trying to send emails using my godaddy account in java. Below is my code.

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

While executing I'm getting the below Exception.

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtpout.secureserver.net , 465; timeout -1; nested exception is: java.net.UnknownHostException: smtpout.secureserver.net

PS:When i use gmail account for authentication its working fine and email sent successfully. When i use godaddy account the exception throws.

Please guide me how to solve this issue...

Thanks in advance...

My configuration in SpringBoot (replace domainname.com to your domainname)

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: "*"

Also I had to add mimeMessageHelper.setFrom("info@domainname.com"); before sending the mail (else it was taking my system name and gave an error) and this setup worked.

Here is complete method that is working absolutely fine for me (I have also used an attachment in the message) :

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 with no Authentication trust certificate regardless

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

requires javax.mail 1.5.2 and greater

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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