2016-04-30 144 views
0

我是新來的jsp,我想從一個jsp頁面發送電子郵件,但它沒有任何stacktrace失敗。這裏是我試過的代碼,不能發送電子郵件從jsp使用Gmail的SMTP服務器

<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
 
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
 
<%@ page import="java.io.*,java.util.*,javax.mail.*"%> 
 
<% 
 
    
 
    //username for [email protected] will be "abc" 
 
    String username = "abc"; 
 
    String password = "password"; 
 
    String result = null; 
 
    
 
    try { 
 
      
 
     Properties props = System.getProperties(); 
 
     props.setProperty("mail.transport.protocol", "smtp"); 
 
     props.setProperty("mail.host", "smtp.gmail.com"); 
 
     props.put("mail.smtp.auth", "true"); 
 
     props.put("mail.smtp.port", "465"); 
 
     props.put("mail.debug", "true"); 
 
     props.put("mail.smtp.socketFactory.port", "465"); 
 
     props.put("mail.smtp.socketFactory.class", 
 
       "javax.net.ssl.SSLSocketFactory"); 
 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
 
    
 
     Session emailSession = Session.getInstance(props, 
 
       new javax.mail.Authenticator() { 
 
        protected PasswordAuthentication getPasswordAuthentication() { 
 
        return new PasswordAuthentication("sender_username","sender_password"); 
 
       } 
 
     }); 
 
    
 
     emailSession.setDebug(true); 
 
     Message message = new MimeMessage(emailSession); 
 
     message.setFrom(new InternetAddress(
 
       "[email protected]")); 
 
     message.setRecipients(Message.RecipientType.TO, 
 
       InternetAddress.parse("[email protected]")); 
 
     message.setSubject("Test mail from Java"); 
 
     message.setText("Hello. this is a test"); 
 
    
 
     Transport transport = emailSession.getTransport("smtps"); 
 
     transport.connect("smtp.gmail.com", username, password); 
 
     transport.sendMessage(message, message.getAllRecipients()); 
 
    
 
     result = "Successfully sent email"; 
 

 
     } catch (MessagingException e) { 
 
     result = "Unable to send email"; 
 
     e.printStackTrace(); 
 
    } 
 
%> 
 

 
<html> 
 
<head> 
 
<title>Send Email using JSP</title> 
 
</head> 
 
<body> 
 
    <center> 
 
     <h1>Send Email using JSP</h1> 
 
    </center> 
 
    <p align="center"> 
 
     <% 
 
      out.println("Result: " + result + "\n"); 
 
     %> 
 
    </p> 
 
</body> 
 
</html>

我從一個博客/教程頁面採取它。由於它沒有打印堆棧跟蹤,所以我也看不到它失敗的地方。它失敗爲 「結果:無法發送電子郵件」

回答

0

希望爲您工作。

 final String username = "[email protected]";//from which you want to send mail 
    final String password = "[email protected]";//password off your mail id 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

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

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]"));//whom you want to send mail 
     message.setSubject("Testing Subject"); 
     message.setText("Dear Mail Crawler," 
      + "\n\n No spam to my email, please!"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
} 
+0

再次感謝。但它仍然失敗,沒有任何堆棧跟蹤:(我添加了一個例外的印刷證實它發生異常。 – Chandrasekar

+0

然後有'jar'的問題..更改jar的版本,然後再試一次 – yash

相關問題