Assuming that you're attaching a binary file....
<pre>
public boolean sendTheEmail()
{
boolean messageSent = true;
try
{
//Get the system properties
Properties properties = new Properties();
//Set the mail server
properties.put("mail.host", getHost());
//Get the session for this property
Session session = Session.getDefaultInstance(properties, null);
//Create email objects
Message message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
//Set the message body and add it to the message
mimeBodyPart.setText(getMessage());
multipart.addBodyPart(mimeBodyPart);
//create a data handler for the binary attachment
mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(new DataHandler(new FileDataSource(getFileName())));
mimeBodyPart.setFileName(getAttachmentName());
//we want to mime-encode the file
mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");
//Add the attachment to the email
multipart.addBodyPart(mimeBodyPart);
//Set the content of our message to the multipart mime message
message.setContent(multipart);
//Set to, from, and subject
message.setFrom(new InternetAddress(getFrom()));
InternetAddress[] toAddress = {new InternetAddress(getTo())};
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject(getSubject());
message.setSentDate(new Date());
//send the email
Transport.send(message);
}
catch(Exception e)
{
messageSent = false;
System.out.println("Error in TextEmailBinaryAttachment: \n" + e);
}
return messageSent;
}
</pre>
I'm not including all of the get/set String (getTo(), getFrom(), etc.)
methods, but this should point you in the right direction.
Walt
---- Smita Kotnis <[EMAIL PROTECTED]> wrote:
> Yes,
> Java Mail doc says how to do this. Please go through the docs.
>
> Smita
>
> Quoting Alireza Nahavandi <[EMAIL PROTECTED]>:
>
> > Hi All,
> >
> > Does anybody have the experience of attaching a file to an email
> in Java
> > ?
> >
> > Thank you.
> >
> > ===========================================================================
> > To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> > JSP-INTEREST".
> > For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> > DIGEST".
> > Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://archives.java.sun.com/jsp-interest.html
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.jsp
> > http://www.jguru.com/faq/index.jsp
> > http://www.jspinsider.com
> >
>
> -------------------------------------------------
> This mail helped a tree grow. Know more at http://green.sify.com
>
> Take the shortest route to success!
> Click here to know how http://education.sify.com
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://archives.java.sun.com/jsp-interest.html
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.jsp
> http://www.jguru.com/faq/index.jsp
> http://www.jspinsider.com
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com