2017-08-28 87 views
0

是否可以使用Java Mail連接到Exchange Online Server(Office 365)?可以使用JavaMail連接Exchange Online Server(Office 365)嗎?

+0

我從來沒有嘗試過,但通過這個頁面它似乎支持所有需要的協議:https://support.microsoft.com/en-us/help/2021880/configuring-outlook-for-microsoft-online -services-mso –

+0

@ ismsankalp89編輯被拒絕。我們不是盲目的。 – EJP

+0

謝謝你的信息。 –

回答

0

是的,這是我從github

import java.util.Date; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

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 SendEmailOffice365 { 

    private static final Logger LOGGER = Logger.getAnonymousLogger(); 

    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]"; 
    private static final String SENHA_CONTA_PADRAO = "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) { 
      LOGGER.log(Level.WARNING, "Erro ao enviar mensagem: " + ex.getMessage(), 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 SendEmailOffice365().sendEmail(); 
    } 

} 
+0

謝謝Yohannes。我認爲,如果您可以連接發送郵件,也可以連接到閱讀收件箱/文件夾。 –

+0

理想情況是的。很難僅支持發送部分。 –

0

微軟有太多的產品與真內相同名稱不同了一個例子!這些JavaMail FAQ instructions應該有所幫助,但是您必須更改主機名。

相關問題