This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Other JSE/JEE APIs and the fly likes FileNotFoundException while sending mail with attachment using javamail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "FileNotFoundException while sending mail with attachment using javamail" Watch "FileNotFoundException while sending mail with attachment using javamail" New topic

FileNotFoundException while sending mail with attachment using javamail

Param Ramli

Joined: Dec 27, 2002
Posts: 2
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,
<b>SendMail with Attachment</b>
<form action="mailer.jsp" method="post">
<table width=50% border=1 cellspacing=0 cellpadding=3 bgcolor=#F2F2F2>
<td width=40%><b>From :</b></td><td><input type="text" name="from"></td>
<td width=40%><b>To :</b></td><td><input type="text" name="to" ></td>
<td width=40%><b>Subject :</b></td><td><input type="text" name="subject" ></td>
<tr><td width=40%><b>Attachment :</b></td><td><input type="file" name="path" ></td>
<td width=40%><b> Message :</b></td><td>
<textarea rows="8" cols="50" name="message"></textarea></td>

<input type="submit" value="Send"></input>

<%@ page language="java" import = "jspbean.MailerBean" %>
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="*"/>


mailer.sendMail(path); %>

<br><center>Email has been sent successfully to <%= to %>.<br>
package jspbean;
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 = "";
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) { = 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;
throw new Exception("Could not send email.");
props = System.getProperties();
session = Session.getDefaultInstance(props, null);

MimeMessage message = new MimeMessage(session);
new InternetAddress(;
message.setFrom(new InternetAddress(this.from));
//create the message part
MimeBodyPart messageBodyPart= new MimeBodyPart();
Multipart multipart= new MimeMultipart();

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


catch (MessagingException e)
throw new Exception(e.getMessage());

/* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if(( == null) ||(this.from==null)||
(this.subject == null) || (this.message == null))
return false;
if(("@") == -1) || (".")
== -1))
return false;
if((this.from.indexOf("@") == -1) || (this.from.indexOf
(".") == -1))
return false;
return true;
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Moving to "Other Java APIs" forum..

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
It is sorta covered in the JavaRanch Style Guide.
subject: FileNotFoundException while sending mail with attachment using javamail