2017-08-03 32 views
0

我試圖使用Gmail smtp服務器從網頁發送電子郵件到我的電子郵件ID。我在stackoverflow上試過各種答案。不幸的是,他們都沒有工作。使用JavaMail API時沒有收到任何迴應

`public void sendMail(String msg){ 

    Properties props= System.getProperties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtps.debug", "true"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 
    //props.put("mail.smtp.ssl.enable", "true"); 
    //props.put("mail.transport.protocol", "smtp"); 

    Authenticator authenticate= new Authenticator(){ 
      protected PasswordAuthentication getPasswordAuthentication(){ 
      return new PasswordAuthentication(username, password); 
      } 
      }; 
    @SuppressWarnings("deprecation") 
    Session mailSession= Session.getInstance(props, authenticate); 
    mailSession.setDebug(true); 

    Message mailMessage= new MimeMessage(mailSession); 

    try { 
     mailMessage.setFrom(new InternetAddress(mailFrom)); 
     mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); 
     mailMessage.setSubject(subject); 
     mailMessage.setText(msg); 

     Transport trans= mailSession.getTransport("smtp"); 
     trans.connect("smtp.gmail.com", username, password); 
     trans.sendMessage(mailMessage, mailMessage.getAllRecipients()); 
     trans.close(); 

    } catch (Exception ex) { 
     Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex); 
    } 

}` 

我想上面寫的代碼

DEBUG: setDebug: JavaMail version 1.5.4 Info: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] Info: DEBUG SMTP: useEhlo true, useAuth true Info: DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

而且,這是我得到的,沒有例外,沒有消息的輸出,而不是連一封電子郵件給我的電子郵件地址。 我參考聯機教程使用JavaMail API 1.5.4 Java 8和GlassFish Server作爲本地主機服務器。

請幫忙!!

+0

修復這些[常見錯誤](https://javaee.github.io/javamail/FAQ#commonmistakes),請按照這些[Gmail說明](https://javaee.github.io/javamail/FAQ#gmail) ,如果它仍然不起作用,請嘗試這些[連接調試技巧](https://javaee.github.io/javamail/FAQ#condebug)。 –

+0

因此它停止嘗試連接? –

+0

@BillShannon感謝您的回覆,我刪除了常見的錯誤。但是這一次我得到這個:javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:465; – Cyrus

回答

0

不是JavaMail,我用它來發送一個SendGrid帳號,但是一些較老的apache http代碼可能會幫助你進行調試。我不知道它是否仍然有效,但應該這樣做。注意端口差異。

SimpleSMTPHeader header; 
    AuthenticatingSMTPClient client; 
    server = "smtp.gmail.com"; 

    client = new AuthenticatingSMTPClient(); 
    client.addProtocolCommandListener(new PrintCommandListener(
              new PrintWriter(System.out), true)); 

    client.connect(server, 587); 
    client.ehlo(client.getLocalAddress().getHostName()); 
    client.execTLS(); 

    if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) 
    { 
     client.disconnect(); 
     System.err.println("SMTP server refused connection."); 
     System.exit(1); 
    } 

    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, "[email protected]", "password"); 


    sender = "[email protected]"; 
    subject = "A subject"; 

    BufferedReader reader = new BufferedReader(new FileReader("html/DocumentReport.html")); 
    client.mail("<[email protected]>"); 
    String receipient = "[email protected]; 

    client.addRecipient(receipient); 

    writer = client.sendMessageData(); 

    header = new SimpleSMTPHeader(sender, receipient, subject); 
    header.addHeaderField("Mime-Version", "1.0"); 
    header.addHeaderField("Content-Type", "text/html; charset=\"ISO-8859-1\""); 
    header.addHeaderField("Content-Transfer-Encoding", "7bit"); 


    writer.write(header.toString()); 
    try { 
     Util.copyReader(reader, writer); 
    } finally { 
     reader.close();    
    } 

    writer.close(); 

    boolean completed = client.completePendingCommand(); 

    client.logout(); 

    client.disconnect(); 
0

最後,我知道了,我現在可以發送電子郵件了。謝謝@BillShanon,@KarlNicholas和Yashashvi Kaushik的支持。 這裏是我提到的JavaMail阿比

Properties props= System.getProperties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.auth", "true"); 

    props.put("mail.smtp.ssl.enable", "true"); 

    Authenticator authenticate= new Authenticator(){ 
      protected PasswordAuthentication getPasswordAuthentication(){ 
      return new PasswordAuthentication(username, password); 
      } 
      }; 
    @SuppressWarnings("deprecation") 
    Session mailSession= Session.getInstance(props, authenticate); 
    mailSession.setDebug(true); 


    Message mailMessage= new MimeMessage(mailSession); 


    try { 
     mailMessage.setFrom(new InternetAddress(mailFrom)); 
     mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); 
     mailMessage.setSubject(subject); 
     mailMessage.setText(msg); 

     Transport trans= mailSession.getTransport("smtp"); 
     trans.connect("smtp.gmail.com", username, password); 
     Transport.send(mailMessage); 

    } catch (Exception ex) { 
     Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex); 
    } 

的版本爲我工作除了這個我停用了Avast的反病毒防火牆,因爲這是不允許我的程序與我的郵件ID連接的代碼。希望,這將適用於其他編碼人員也有同樣的問題。

+0

你的代碼包含幾個[常見的JavaMail錯誤](https://javaee.github.io/javamail/FAQ#commonmistakes)。請更新您的文章中的代碼,以便複製代碼的下一個人將獲得正確的代碼。謝謝。 –

相關問題