2016-09-28 214 views
0

我正在使用下面的代碼從使用java的Outlook發送電子郵件。但是得到錯誤。如何使用java api使用Outlook發送電子郵件

CODE:

public static void mail(){ 
     // TODO Auto-generated method stub 
     //String host="POKCPEX07.corp.absc.local"; 
     String host="POKCPEX07.corp.absc.local"; 
     final String user="[email protected]"; 
     String to="[email protected]"; 

     //Get the session object 
     Properties props = new Properties(); 
     props.put("mail.smtp.host",host); 
     props.put("mail.smtp.auth", "false"); 
     props.put("mail.smtp.port", "587"); 


     Session session=Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]","******"); 
      } 
      }); 
     session.setDebug(true); 

     try { 
      MimeMessage message = new MimeMessage(session); 
      message.saveChanges(); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
      message.setSubject("Test mail"); 
      message.setText("This is test mail."); 

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

      System.out.println("message sent successfully..."); 
     } 
     catch (MessagingException e) {e.printStackTrace();} 

    } 
} 

錯誤:

 
javax.mail.MessagingException: Could not connect to SMTP host: POKCPEX07.corp.absc.local, port: 587; 
    nested exception is: 
    java.net.SocketException: Permission denied: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322) 
    at javax.mail.Service.connect(Service.java:258) 
    at javax.mail.Service.connect(Service.java:137) 
    at javax.mail.Service.connect(Service.java:86) 
    at javax.mail.Transport.send0(Transport.java:150) 
    at javax.mail.Transport.send(Transport.java:80) 
    at TestEmail.mail(TestEmail.java:50) 
    at TestEmail.main(TestEmail.java:16) 
+0

您的電子郵件服務器是否配置爲通過SMTP協議連接到它? – prabodhprakash

+0

我不認爲它已配置。要做到這一點,將需要做些什麼。 但即使使用Gmail,我也無法發送。獲取「權限被拒絕」的錯誤。 –

回答

1
package com.sendmail; 

import java.util.Date; 
import java.util.Properties; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendAttachmentInEmail { 


    private static final String SERVIDOR_SMTP = "smtp.office365.com"; 
    private static final int PORTA_SERVIDOR_SMTP = 587; 
    private static final String CONTA_PADRAO = "[email protected]"; //Cofig Mail Id 
    private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password 

    private final String from = "[email protected]"; 
    private final String to = "[email protected]"; 

    private final String subject = "Teste"; 
    private final String messageContent = "Teste de Mensagem"; 

    public void sendEmail() { 
     final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() { 

      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO); 
      } 

     }); 

     try { 
      final Message message = new MimeMessage(session); 
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      message.setFrom(new InternetAddress(from)); 
      message.setSubject(subject); 
      message.setText(messageContent); 
      message.setSentDate(new Date()); 
      Transport.send(message); 
     } catch (final MessagingException ex) { 
      System.out.println(" "+ex); 
     } 
    } 

    public Properties getEmailProperties() { 
     final Properties config = new Properties(); 
     config.put("mail.smtp.auth", "true"); 
     config.put("mail.smtp.starttls.enable", "true"); 
     config.put("mail.smtp.host", SERVIDOR_SMTP); 
     config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP); 
     return config; 
    } 

    public static void main(final String[] args) { 
     new SendAttachmentInEmail().sendEmail(); 
    } 

} 
+0

嘗試了上面的代碼片段與所需的罐子。獲取以下錯誤: 線程「main」中的異常java.lang.RuntimeException:javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:587; 嵌套的例外是: \t java.net.SocketException異常:權限被拒絕:連接 \t在TestEmail.main(TestEmail.java:40) 產生的原因:javax.mail.MessagingException的:無法連接到SMTP主機:smtp.gmail .com,端口:587; 嵌套的異常是: \t java.net.SocketException:權限被拒絕:連接 –

+0

更改屬性設置: props.put(「mail.smtp.host」,「smtp.gmail.com」);到 props.put(「mail.smtp.host」,「outlook.office365.com」); 引用請點擊鏈接:http://stackoverflow.com/questions/20613569/java-mail-api-send-emails-via-corporate-outlook-acount –

+0

按照此鏈接工作正常。 https://gist.github.com/brunocesarsilva/12a529f7f752f2853b9f –

0

正如您發佈的上述意見,它看起來像沒有配置您的SMTP並通過尋找你的異常 - 您使用Gmail中。按照link配置您的SMTP。

相關問題