2012-09-20 41 views
0

我建立在Java的電子郵件應用程序,到目前爲止,我嘗試使用這個,我正在上運行該代碼中沒有錯誤,但經過我編譯和運行代碼的問題是,淨豆IDE只是表明我的計劃正在運行,但是當我檢查郵箱時,沒有郵件收到。任何人都可以解釋爲什麼發生這種情況?爲什麼我的郵件沒有發送?

而且我打印的消息「發送成功」消息結尾,但不知何故沒有得到打印的消息,我也弄不清是什麼錯誤,任何幫助,將不勝感激,謝謝。如果你的代理/防火牆後面運行

public class Email 
    public static void main(String[] args) { 
    // TODO code application logic here 


    String[] to = {"[email protected]"}; 
    String from = "[email protected]"; 
    String host = "smtp.gmail.com"; 
      String user_name = "[email protected]"; 
      String password = "xyz"; 

    try{ 
    Properties properties = System.getProperties(); 
    properties.setProperty("smtp.gmail.com", "imaps"); 
      properties.put("smtp.starttls.enable", "true"); 
      properties.put("mail.smtp.host",host); 
      properties.put("mail.smtp.user",user_name); 
      properties.put("mail.smtp.password", password); 
      properties.put("mail.smtp.port", "465"); 
      properties.put("mail.smtp.auth", "true"); 


      //properties.put 
    Session session = Session.getDefaultInstance(properties,null); 
      //Store store = session.getStore("imaps"); 



     MimeMessage message = new MimeMessage(session); 
     // basically stores one or more addresses , whom the mail has to be  sent 
     //InternetAddress[] send_to = { new InternetAddress(to) }; 
     //InternetAddress[] send_from = { new InternetAddress(from) }; 
        InternetAddress[] toAddress = new InternetAddress[to.length]; 

        for(int i = 0; i < to.length ;i++) 
        { 
         toAddress[i] = new InternetAddress(to[i]); 
        } 

        for(int i = 0 ; i < toAddress.length ; i++){ 
     message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
        } 

     message.setFrom(new InternetAddress(from)); 
     message.setSubject("Testing email"); 
     message.setText("this is a test"); 
        Transport transport = session.getTransport("smtp"); 
        transport.connect(host, user_name, password); 
     transport.send(message); 
        transport.close(); 


        System.out.println("Message successfully sent"); 


    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

}

回答

1

試試這個代碼

public class SendMail { 

    String host, port, emailid,username, password; 
    Properties props = System.getProperties(); 
    Session l_session = null; 

    public SendMail() { 
     host = "smtp.mail.yahoo.com"; 
     port = "587"; 
     emailid = "[email protected]"; 
     username = "a"; 
     password = "pwd"; 

     emailSettings(); 
     createSession(); 
     sendMessage("[email protected]", "[email protected]","Test","test Mail"); 
    } 

    public void emailSettings() { 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "false"); 
     props.put("mail.smtp.port", port); 
//  props.put("mail.smtp.socketFactory.port", port); 
//  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
//  props.put("mail.smtp.socketFactory.fallback", "false"); 

    } 

    public void createSession() { 

     l_session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, password); 
        } 
       }); 

     l_session.setDebug(true); // Enable the debug mode 

    } 

    public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) { 
     //System.out.println("Inside sendMessage 2 :: >> "); 
     try { 
      //System.out.println("Sending Message *********************************** "); 
      MimeMessage message = new MimeMessage(l_session); 
      emailid = emailFromUser; 
      //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid); 
      //message.setFrom(new InternetAddress(emailid)); 
      message.setFrom(new InternetAddress(this.emailid)); 

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); 
      message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail)); 
      message.setSubject(subject); 
      message.setContent(msg, "text/html"); 

      //message.setText(msg); 
      Transport.send(message); 
      System.out.println("Message Sent"); 
     } catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }//end catch block 
     return true; 
    } 

} 
+0

嘿我試過使用上面的代碼,但我得到以下錯誤。 http://dl.dropbox.com/u/53861781/Links/link.txt,我已經指定了用戶名,但爲什麼它仍然顯示我這個錯誤? – Sam

1

您可能需要設置代理

System.setProperty("http.proxyHost", "myProxyServer.com"); 
System.setProperty("http.proxyPort", "80"); 
0

謝謝你的人,看起來像我錯過了

props.put(「mail.smtp.socketFactory.port」,「587」);

部分,添加了這個,它的工作正常。

+0

[不要那樣做。(http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)的[JavaMail的FAQ告訴你如何連接到Gmail(HTTP:/ /www.oracle.com/technetwork/java/javamail/faq/index.html#gmail)。 –

+0

比爾香農,現在工作,但無論如何感謝。 – Sam

+0

您可能要聽他的,他所做的畢竟寫的JavaMail(https://kenai.com/hg/javamail~mercurial),如果你讀他與之鏈接的常見問題告訴你爲什麼你不需要的屬性對於socketFactory了。 – Chase

相關問題