Solved

Sending Attachments with emails

Posted on 2002-04-02
7
196 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

23 Experts available now in Live!

Get 1:1 Help Now