Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sending Attachments with emails

Posted on 2002-04-02
7
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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.

688 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