Sending Attachments with emails

 I am trying to send an attachment with an email sent from a servlet. I can send normal emails fine (i.e. emails without attachments). However I'm not to sure how to do so with an attachment.
Here is my code for an email without an attachment. Could you let me know how to modify it to include the sending of an email..

My Code:

  public void emailAttachmentTestRequest(HttpServletRequest request,
                                HttpServletResponse response,
                                HttpSession httpSession) throws ServletException, IOException {

    System.err.println("--- emailing AttachmentTest");

    String from = null;
    String to = null;
    SmtpClient smtp = null;
    PrintStream msg = null;

    smtp = new SmtpClient("");
    from = "";
    to = "";

    msg = smtp.startMessage();

    try {
      msg.println("From: " + from);
      msg.println("To: " + to);
      msg.println("Subject: Testing Email");
      msg.println("This is the Test text");
    catch (IOException e) {
      System.err.println("err sending email" + e.getMessage());


Who is Participating?
msterjevConnect With a Mentor Commented:
Just download JavaMail API. Look at the MimeMultipart and BodyPart classes. There is tutorial in pdf format and full example code for sending mail atachments:

Be cool and use this standard J2EE API unstead of na old one.
seoirseAuthor Commented:
From reading an article. It loks to me that using SMTPClient it isnot possible to send attachments with this. If you know how to use the JavaMail class to do so then could you let me know also.
If you can wait reading the the whole tutorial here is the whole example of using JavaMail and sending attachments:,,12395_618471,00.html

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The points, please!
The points, please!
seoirseAuthor Commented:
Points are yours msterjev
You welcome!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.