Solved

Sending Attachments with emails

Posted on 2002-04-02
7
197 Views
Last Modified: 2013-11-24
Hi,
 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("smtp.test.com");
    from = "test@test.com";
    to = "test@test.com";

    smtp.from(from);
    smtp.to(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");
      smtp.closeServer();
    }
    catch (IOException e) {
      System.err.println("err sending email" + e.getMessage());
    }
  }

 


0
Comment
Question by:seoirse
  • 5
  • 2
7 Comments
 

Author Comment

by:seoirse
ID: 6913251
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.
0
 
LVL 3

Accepted Solution

by:
msterjev earned 100 total points
ID: 6913488
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:

http://java.sun.com/products/javamail/index.html

Be cool and use this standard J2EE API unstead of na old one.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6913521
If you can wait reading the the whole tutorial here is the whole example of using JavaMail and sending attachments:

http://softwaredev.earthweb.com/java/sdjjavase/article/0,,12395_618471,00.html

Enjoy.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Expert Comment

by:msterjev
ID: 6915165
The points, please!
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6915737
The points, please!
0
 

Author Comment

by:seoirse
ID: 6917540
Points are yours msterjev
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6917584
You welcome!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now