2015-10-06 46 views
2

我正在使用java郵件api發送郵件。我可以使用Google帳戶發送郵件,但面臨使用SSL從我的域發送的某些問題。無法從我的主機發送郵件

這是實施,請讓我知道我缺乏的地方。

// Create all the needed properties 
     Properties connectionProperties = new Properties(); 
     // SMTP host 
     connectionProperties.put("mail.smtp.host", "abc.example.com"); 
     // Is authentication enabled 
     connectionProperties.put("mail.smtp.auth", "true"); 
     // Is StartTLS enabled 
     connectionProperties.put("mail.smtp.starttls.enable", "true"); 
     // SSL Port 
     connectionProperties.put("mail.smtp.socketFactory.port", "465"); 
     // SSL Socket Factory class 
     connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

     // SMTP port, the same as SSL port :) 
     connectionProperties.put("mail.smtp.port", "465"); 

     // Create the session 
     Session session = Session.getDefaultInstance(connectionProperties, new javax.mail.Authenticator() 
     { // Define the authenticator 
      protected PasswordAuthentication getPasswordAuthentication() 
      { 
       return new PasswordAuthentication("[email protected]", "abcxdsjdf123"); 
      } 
     }); 

     System.out.println("done!"); 

     // Create and send the message 
     try 
     { 
      // Create the message 
      Message message = new MimeMessage(session); 
      // Set sender 
      message.setFrom(new InternetAddress("[email protected]")); 
      // Set the recipients 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
      // Set message subject 
      message.setSubject("Hello from Test"); 
      // Set message text 
      message.setText("This is test mail;)"); 

      System.out.print("Sending message..."); 

      // Send the message 
      Transport.send(message); 

      System.out.println("done!"); 
     } 
     catch (MessagingException e) 
     { 
      System.out.println(e.toString()); 
     } 

下面是我收到的例外:

javax.mail.MessagingException: Exception reading response; 
    nested exception is: 
    java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 

任何形式的幫助將是明顯的。 在此先感謝。

+0

我想你do't提及關於協議props.put( 「SMTP」 「mail.transport.protocol。」);把這個放在末尾 – Sree

+0

你現在使用哪個主機? –

+0

我使用magemojo主機 –

回答

0

試試這個..

public class SentMail { 
     public boolean deliverMail(String username, String email, String subject, String content) { 
      boolean sent = false; 
      try { 
       String delim = "\n\n"; 
       String text = "Hi ," + delim + content + delim + "Thank you ," + delim + username + "\n" + email; 
       String from = "from_email"; 
       String pass = "from_pass"; 
       String to = "to_email"; 
       <!--Your host--> 
       String host = "smtp.zoho.com"; 
       <!--Host port--> 
       String port = "465"; 
       Properties properties = System.getProperties(); 
       properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
       properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
       properties.setProperty("mail.smtp.socketFactory.fallback", "false"); 
       properties.setProperty("mail.smtp.socketFactory.port", port); 
       properties.put("mail.smtp.startssl.enable", "true"); 
       Session session = Session.getDefaultInstance(properties); 
       MimeMessage message = new MimeMessage(session); 
       message.setFrom(new InternetAddress(from)); 
       message.addRecipient(Message.RecipientType.TO, 
         new InternetAddress(to)); 
       message.setSubject(subject); 
       message.setText(text); 
       Transport transport = session.getTransport("smtp"); 
       transport.connect(host, from, pass); 
       transport.sendMessage(message, message.getAllRecipients()); 
       transport.close(); 
       sent = true; 
      } catch (Exception e) { 

      } 
      return sent; 

     } 
    } 
+0

感謝您的回覆,但我得到javax.mail.MessagingException:無法連接到SMTP主機:abc.magemojo.com,端口:25; 嵌套的異常是: \t javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:未找到證書路徑的信任錨點。 –

+0

看起來你沒有正確配置主機和端口.. –

+0

我的主機是example.magemojo.com和端口是465 –

相關問題