2016-07-28 45 views
1

我已經通過其他許多帖子搜索與類似問題的用戶,但我似乎無法讓它工作。用JavaMail發送電子郵件到我的作品的電子郵件服務器

package testingstuff; 
import java.io.*; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.Message.RecipientType; 
import javax.mail.internet.*; 
import javax.activation.*; 
public class test { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    System.out.println("Hello World"); 


    final String user = "my work ID"; 
    final String pw = "my work Password"; 

    Properties props = new Properties(); 



    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "server I got from our outlook app"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(user, pw); 
       } 
      }); 

    session.setDebug(true); 

    try { 
     Message msg = new MimeMessage(session); 
     String from = "[email protected]"; 
     String to = "[email protected]"; 
     msg.setFrom(new InternetAddress(from)); 
     msg.setRecipient(RecipientType.TO, new InternetAddress(to)); 
     msg.setSubject("TEST"); 
     msg.setText("TESTING"); 

     //Transport trans = session.getTransport("smtp"); 
     //trans.connect("smtp-mail.outlook.com", 25, user, pw); 
     Transport.send(msg); 
    } 
    catch (Exception e) { 
     System.out.println(e); 
    } 

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

} 

我用java 1.8,JavaMail的報告說,它是版本1.4.7

這裏從會話調試輸出:

Hello World 
DEBUG: setDebug: JavaMail version 1.4.7 

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 

DEBUG SMTP: useEhlo true, useAuth true 

DEBUG SMTP: useEhlo true, useAuth true 

DEBUG SMTP: trying to connect to host "[my work email server]", port 587, isSSL false 

javax.mail.MessagingException: Could not connect to SMTP host: [my work email server], port: 587; 

嵌套的例外是:

java.net.ConnectException: Connection timed out: connect 

完成

我使用的電子郵件服務器來自Outlook應用程序。我進入了帳戶設置,選擇了我的帳戶並選擇了更改。這將打開一個包含電子郵件配置的窗口,其中包含服務器地址。

+0

我試過之前,它也沒有工作。我從工作網絡發送它,所以它被阻止。我建議通過電子郵件發送到私人Gmail或其他東西,看看是否可行 –

+0

也許端口號可能會導致問題。例如嘗試「80」。 – pilkington

回答

0

嘗試下面的代碼,只是把用戶名,密碼,以從,主

import java.util.*; 

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

public class SendEmail 
{ 
    public static void main(String [] args) 
    {  
      String username = ""; 
      String password = ""; 
      String to = ""; 
      String from = ""; 
      String host = ""; 

      Properties properties = System.getProperties(); 


      properties.setProperty("mail.smtp.host", host); 
      properties.setProperty("mail.smtp.user", username); 
      properties.setProperty("mail.smtp.password", password); 
      properties.setProperty("mail.smtps.auth", "true"); 
      Session session = Session.getDefaultInstance(properties); 

      try{ 
      MimeMessage message = new MimeMessage(session); 

      message.setFrom(new InternetAddress(from)); 

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 

      message.setSubject("This is the Subject Line!"); 

      BodyPart messageBodyPart = new MimeBodyPart(); 

      messageBodyPart.setText("This is message body"); 

      Multipart multipart = new MimeMultipart(); 

      multipart.addBodyPart(messageBodyPart); 




      message.setContent(multipart); 



      message.saveChanges(); 
      Transport transport = session.getTransport("smtp"); 
      transport.connect(host, username, password); 
      transport.sendMessage(message, message.getAllRecipients()); 
      transport.close(); 
      System.out.println("Sent message successfully...."); 
      }catch (MessagingException mex) { 
      mex.printStackTrace(); 
      } 
} 



} 
+0

你好,它似乎仍然無法正常工作。 我認爲這可能是我可能有錯誤的端口,或者我可能需要一些額外的安全措施才能進入。我在工作時通過電子郵件發送了IT支持,詢問他們需要什麼憑據和設置才能使這項工作。儘管謝謝您的幫助! – KevinCamacho

相關問題