0

I want to send email using jsp. I have develop this small code to send email. I have added all require library like java mail API and JAF. But still it is not working. I don't know whats going wrong in this. it shows me Error: unable to send message.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
<% String result; 
   String to = "kudale.ashish1992@gmail.com"; 
   String from = "kudale.ashish1992@yahoo.com"; 
   String host = "smtp.mail.yahoo.com"; 
   Properties properties = System.getProperties(); 
   properties.setProperty("mail.smtp.host", host); 
   properties.setProperty("mail.user", "Username");
   properties.setProperty("mail.password", "Paasword");
   //String to = request.getParameter("to"); 
   //String from = request.getParameter("from");
   String subject = request.getParameter("subject");
   String messageText = request.getParameter("body");
   Session mailSession = Session.getDefaultInstance(properties); 
   try{ 
       MimeMessage message = new MimeMessage(mailSession);
       message.setFrom(new InternetAddress(from)); 
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       BodyPart messageBodyPart = new MimeBodyPart(); 
       messageBodyPart.setText("This is message body"); 
       Multipart multipart = new MimeMultipart(); 
       multipart.addBodyPart(messageBodyPart); 
       messageBodyPart = new MimeBodyPart(); 
       String filename = "file.txt"; 
       DataSource source = new FileDataSource(filename); 
       messageBodyPart.setDataHandler(new DataHandler(source)); 
       messageBodyPart.setFileName(filename); 
       multipart.addBodyPart(messageBodyPart);   
       message.setContent(multipart ); 
       Transport.send(message); 
       String title = "Send Email"; 
       result = "Sent message successfully...."; 
    }
    catch (MessagingException mex) { 
       mex.printStackTrace(); 
       result = "Error: unable to send message...."; 
   }
 %> 
<html> 
<head> 
<title>Send email</title> 
</head> 
<body> 
<center> <h1>Send Attachement Email using JSP</h1> </center> 
<p align="center"> 
<% out.println("Result: " + result + "\n"); %> 
</p>
</body>
</html>
Ashish Kudale
  • 1,230
  • 1
  • 27
  • 51

1 Answers1

1

I have tried this on my machine two errors I got.

First, You misspelled properties here

roperties.setProperty("mail.password", "Paasword");

Second, Access is denied error for file.txt. I managed to give a path to it and it worked.

RESULT : It showed me message sent successfully.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
<% String result; 
   String to = "kudale.ashish1992@gmail.com"; 
   String from = "kudale.ashish1992@yahoo.com"; 
   String host = "localhost"; 
   Properties properties = System.getProperties(); 
   properties.setProperty("mail.smtp.host", host); 
   properties.setProperty("mail.user", "Username");
   properties.setProperty("mail.password", "Paasword");
   //String to = request.getParameter("to"); 
   //String from = request.getParameter("from");
   String subject = request.getParameter("subject");
   String messageText = request.getParameter("body");
   Session mailSession = Session.getDefaultInstance(properties); 
   try{ 
       MimeMessage message = new MimeMessage(mailSession);
       message.setFrom(new InternetAddress(from)); 
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       BodyPart messageBodyPart = new MimeBodyPart(); 
       messageBodyPart.setText("This is message body"); 
       Multipart multipart = new MimeMultipart(); 
       multipart.addBodyPart(messageBodyPart); 
       messageBodyPart = new MimeBodyPart(); 
       String filename = "file.txt"; 
       DataSource source = new FileDataSource(filename); 
       messageBodyPart.setDataHandler(new DataHandler(source)); 
       messageBodyPart.setFileName(filename); 
       multipart.addBodyPart(messageBodyPart);   
       message.setContent(multipart ); 
       Transport.send(message); 
       String title = "Send Email"; 
       result = "Sent message successfully...."; 
    }
    catch (MessagingException mex) { 
       mex.printStackTrace(); 
       result = "Error: unable to send message...."; 
   }
 %> 
<html> 
<head> 
<title>Send email</title> 
</head> 
<body> 
<center> <h1>Send Attachement Email using JSP</h1> </center> 
<p align="center"> 
<% out.println("Result: " + result + "\n"); %> 
</p>
</body>
</html>
Asfab
  • 398
  • 6
  • 11
Vinayak Pingale
  • 1,315
  • 8
  • 19
  • still not working. I have added full extracted folder of javamail API, and JAF. should I only add jar files. – Ashish Kudale Jan 18 '14 at 17:45
  • have you got mail ? Its time now to share stack trace. – Vinayak Pingale Jan 18 '14 at 17:46
  • You need to add mail.jar and activation.jar files in your CLASSPATH. All the jar files from Java Email API package and JAF package ara available in CLASSPATH. – Vinayak Pingale Jan 18 '14 at 18:02
  • not working add library propery. enable smtp port, enble TLS, changed my host to **smtp.mail.yahoo.com**. still not woking – Ashish Kudale Jan 19 '14 at 05:04
  • Share stack trace . how to see it ? What applicaton server are you using. Tomcat,jboss. which one? – Vinayak Pingale Jan 19 '14 at 05:33
  • GlassFish Sever. what is stack trace? and how to share that? – Ashish Kudale Jan 19 '14 at 05:49
  • stack trace are like server logs. which identifies what kind of exception it is throwing. – Vinayak Pingale Jan 19 '14 at 05:53
  • Refer http://stackoverflow.com/questions/8468608/where-can-i-find-glassfish-logs-on-windows-7,.... http://stackoverflow.com/questions/13835913/location-of-glassfish-server-logs – Vinayak Pingale Jan 19 '14 at 05:54
  • I read above links. but I don't know how to over come this issue. `C:\Program Files\glassfish-3.1.2.2\glassfish\domains\domain1\logs` the logs folder is empty. – Ashish Kudale Jan 19 '14 at 08:02
  • I found server.logs file inside `\AppData\Roaming\NetBeans\7.2\config\GF3\domain1\logs` it shows me error IOException: Access is denied. and some lines below that starts with at java.io..., at org.apache.., at com.sun.. and many more. what to do now. – Ashish Kudale Jan 19 '14 at 08:24
  • ya i got that error too. I think its not able to access your file.txt. Location of file.txt? – Vinayak Pingale Jan 19 '14 at 08:27
  • String filename = "D:/BPM/SBPM/webapps/deploy/svm/helo/file.txt"; I gave it like this. – Vinayak Pingale Jan 19 '14 at 08:36
  • Have you got any luck? – Vinayak Pingale Jan 19 '14 at 14:42
  • are you sure there no error in code. I think changing of the file path wont working. – Ashish Kudale Jan 19 '14 at 15:45
  • what is the path name you gave?. what is coming in stack trace access is denied but for what. – Vinayak Pingale Jan 19 '14 at 15:46
  • OK I have copied error from `server.logs` and pasted below **[#|2014-01-19T21:22:31.857+0530|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=97;_ThreadName=Thread-2;|javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target** – Ashish Kudale Jan 19 '14 at 16:10
  • props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); Try both of them with gmail account id. – Vinayak Pingale Jan 19 '14 at 18:27
  • Ty Referring this for SSL handshake. http://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun-security-validator-validatore – Vinayak Pingale Jan 19 '14 at 18:33
  • now error is reduced. `com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.` I tried with `properties.setProperty("mail.smtp.auth", "true");` but still not working. – Ashish Kudale Jan 20 '14 at 05:42
  • Its very close now.Refer http://stackoverflow.com/questions/11189320/how-to-solve-com-sun-mail-smtp-smtpsendfailedexception-in-java – Vinayak Pingale Jan 20 '14 at 05:47
  • no errors but still have exception in console `Interrupting idle Thread: http-thread-pool-8080(1)` I try to search google but didn't got proper solution. – Ashish Kudale Jan 21 '14 at 18:31
  • Size of file.txt? you are attaching is it huge. Its a known issue of glassfish. Go to glassfish bin. open asadmin console and write this command. `asadmin set server-config.network-config.protocols.protocol.http-listener-1.http.request-timeout-seconds=900` – Vinayak Pingale Jan 22 '14 at 07:04
  • the text file is only 1 Kb. I tried with above now it create new .log of each each execution process. error is still same `Interrupting idle Thread: http-thread-pool-8080(1)` & `asadmin command not found.` – Ashish Kudale Jan 22 '14 at 13:47
  • Try wrting this in asadmin `set server-config.network-config.protocols.protocol.http-listener-1.http.request-tim‌​eout-seconds=900` – Vinayak Pingale Jan 22 '14 at 13:52
  • command works but code doesn't, why this happening with me. I want to use it in my project. I think I need to find any other alternative for this. – Ashish Kudale Jan 22 '14 at 14:19
  • I dont think it will create any problem in other application server. which application server does your project use? – Vinayak Pingale Jan 22 '14 at 14:29
  • don't know but most probably glassfish. thank you for your great help. – Ashish Kudale Jan 22 '14 at 14:42
  • use this, can help in case of any question https://code.google.com/p/simple-java-mail/ – zeddarn Mar 06 '14 at 13:23