2016-04-30 5 views
0

Ich bin neu in JSP, ich versuche, E-Mails von einer JSP-Seite senden, aber es scheitert ohne StackTrace. Hier ist der Code Ich habe versucht,nicht in der Lage, E-Mails von JSP mit Gmail SMTP-Server zu senden

<%@ 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>

ich es von einem Blog/Tutorial-Seite genommen haben. Da der Stack-Trace nicht gedruckt wird, konnte ich nicht sehen, wo er ebenfalls fehlschlägt. Es schlägt fehl als "Ergebnis: E-Mail kann nicht gesendet werden"

Antwort

0

hoffe, dass diese Arbeit für Sie.

 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

Danke nochmal. Aber es schlägt immer noch ohne Stacktrace :(Ich habe einen Ausdruck in Ausnahme hinzugefügt, die bestätigt, dass es an die Ausnahme geht. – Chandrasekar

+0

dann gibt es ein Problem von 'jar' .. ändern Sie die Version von jar und versuchen Sie es erneut – yash

Verwandte Themen