This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

FileNotFoundException while sending Mail as attachment using javamail  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
I have used a jsp page which calls a java bean to send mail with attachment.The bean uses Javamail API's to do this.But i encountered a FileNotFoundException when i did run the jsp file ,even as the file selected to be attached was present.

Please inform me where has it gone wrong.
i have enclosed my files below,
sendMailAttachment.html
--------------------------
<html>
<body>
<center>
<b>SendMail with Attachment</b>
<form action="mailer.jsp" method="post">
<table width=50% border=1 cellspacing=0 cellpadding=3 bgcolor=#F2F2F2>
<tr>
<td width=40%><b>From :</b></td><td><input type="text" name="from"></td>
</tr>
<tr>
<td width=40%><b>To :</b></td><td><input type="text" name="to" ></td>
</tr>
<tr>
<td width=40%><b>Subject :</b></td><td><input type="text" name="subject" ></td>
</tr>
<tr><td width=40%><b>Attachment :</b></td><td><input type="file" name="path" ></td>
</tr>
<tr>
<td width=40%><b> Message :</b></td><td>
<textarea rows="8" cols="50" name="message"></textarea></td>
</td>

</tr>
</table>
<br>
<input type="submit" value="Send"></input>
</center>
</form>
</div>
</body>
</html>

mailer.jsp
-----------
<%@ page language="java" import = "jspbean.MailerBean" %>
<html>
<body>
<%
String to=request.getParameter("to");
String from=request.getParameter("from");
String message=request.getParameter("message");
String subject=request.getParameter("subject");
String path=request.getParameter("path");
%>

<jsp:useBean id="mailer" class="jspbean.MailerBean">
<jsp:setProperty name="mailer" property="*"/>
</jsp:useBean>
<%

if(path.length()==0)
path=null;

out.println(path);
mailer.sendMail(path); %>

<br><center>Email has been sent successfully to <%= to %>.<br>
</div>
</body>
</html>

MailerBean.java
------------------
package jspbean;
import java.io.*;
import java.util.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
public final class MailerBean extends Object implements Serializable {
/* Bean Properties */
private String to = null;
private String from = null;
private String host = "192.168.1.1";
private String subject = null;
private String message = null;

//private String path = null;

public static Properties props = null;
public static Session session = null;



/* Setter Methods */
public void setTo(String to) {
this.to = to;
}
public void setFrom(String from) {
this.from = from;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setMessage(String message) {
this.message = message;
}
//public void setfilepath(String path) {
//this.path = path;
//}
/* Sends Email */
public void sendMail(String path) throws Exception
{
String filepath=path;
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try
{
props = System.getProperties();
props.put("mail.smtp.host",host);
session = Session.getDefaultInstance(props, null);

MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
if(filepath!=null)
{
//create the message part
MimeBodyPart messageBodyPart= new MimeBodyPart();
messageBodyPart.setText(this.message);
Multipart multipart= new MimeMultipart();
multipart.addBodyPart(messageBodyPart);


//part two is attachment
messageBodyPart= new MimeBodyPart();
FileDataSource source= new FileDataSource(filepath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filepath);
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

}
else
{
message.setText(this.message);
}
Transport.send(message);
}
catch (MessagingException e)
{
throw new Exception(e.getMessage());
}

}
/* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if((this.to == null) ||(this.from==null)||
(this.subject == null) || (this.message == null))
return false;
if((this.to.indexOf("@") == -1) || (this.to.indexOf(".")
== -1))
return false;
if((this.from.indexOf("@") == -1) || (this.from.indexOf
(".") == -1))
return false;
return true;
}
}
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the JSP forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!