Solved

Sending Attachments with emails

Posted on 2002-04-02
7
201 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
arguments to jar 5 35
JAVA API design with micro service cloud in mind 1 49
Why doesn't this text field show up on my Applet frame? 2 20
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

828 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