Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 27th, 2005, 02:24 AM
Registered User
 
Join Date: Apr 2005
Location: Delhi, Delhi, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Attach multiple files in mail using jsp

Hey!
Hello to every body i m first time on this site...
anyway i need a code in jsp,servlet through which i can attch multiple file in a mail.
i hav a program which can only attach 1 file.
so if hav code then eply me as soon as possible.


Thanx in Advance
Deepak Goel

Reply With Quote
  #2 (permalink)  
Old May 31st, 2005, 06:30 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can try my Mail Module, then use it in Web pages.

Code:
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

public class JavaMailModule {
  private Session session = null;
  private Message meassage;
  private Multipart multipart;
  private StringBuffer stringBuffer;
  public static String smtpServer;
  private String sender;
  private String[] reciver;
  private String subject;
  private String content;
  private String[] paths;
  private Properties properties;

  private void setSmtpServer(String smtpServer) {
    this.smtpServer = smtpServer;
  }

  public String getSmtpServer() {
    return this.smtpServer;
  }

  private void setSender(String sender) {
    this.sender = sender;
  }

  public String getSender() {
    return this.sender;
  }

  private void setReciver(String[] reciver) {
    this.reciver = reciver;
  }

  public String[] getReciver() {
    return this.reciver;
  }

  private void setSubject(String subject) {
    this.subject = subject;
  }

  private void setContent(String content) {
    this.content = content;
  }

  private void setAttachPaths(String[] paths) {
    this.paths = paths;
  }

  public void connectionSetting(String smtpServer, boolean isDebugging) {
    setSmtpServer(smtpServer);
    properties = new Properties();
    properties.put("mail.smtp.host", this.smtpServer);
    session = Session.getDefaultInstance(properties, null);
    session.setDebug(isDebugging);
  }

  public void send(String sender, String[] reciver, String subject,
                   String content, String[] paths) {
    setSender(sender);
    setReciver(reciver);
    setSubject(subject);
    setContent(content);
    setAttachPaths(paths);

    try {
      this.meassage = new MimeMessage(session);
      InternetAddress from = new InternetAddress(this.sender);
      this.meassage.setFrom(from);
      InternetAddress[] address = null;

      if (this.reciver != null) {
        address = new InternetAddress[this.reciver.length];

        for (int i = 0; i < this.reciver.length; i++) {
          address[i] = new InternetAddress(this.reciver[i]);
        }
        this.meassage.setRecipients(Message.RecipientType.TO, address);
      }
      this.meassage.setSubject(this.subject);
      this.stringBuffer = new StringBuffer();
      this.stringBuffer.append(this.content);
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setContent(new String(this.stringBuffer), "text/html;charset=Big5");
      this.multipart = new MimeMultipart();
      this.multipart.addBodyPart(mbp1);

      if (this.paths != null) {
        MimeBodyPart mbp2 = null;
        File file = null;
        FileDataSource fds = null;

        for (int i = 0; i < this.paths.length; i++) {
          mbp2 = new MimeBodyPart();
          String filename = this.paths[i];
          file = new File(this.paths[i]);

          if (file.exists()) {
            fds = new FileDataSource(file);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
            this.multipart.addBodyPart(mbp2);
          }
        }
        file = null;
      }

      try {
        this.meassage.setContent(this.multipart);
        this.meassage.setSentDate(new Date());
        Transport.send(this.meassage);
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    catch (MessagingException mex) {
      mex.printStackTrace(System.err);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
Reply With Quote
  #3 (permalink)  
Old October 24th, 2007, 12:12 PM
Registered User
 
Join Date: Oct 2007
Location: bangalore, karnataka, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can anyone tell me how to attach multiple files in mail using jsp and struts??
yui0329 has provided java code, but i want jsp part of it.

Thanks in advance.
SJS.

sjs
Reply With Quote
  #4 (permalink)  
Old October 26th, 2007, 12:19 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can try by providing multiple file elements in the jsp form and create an array of FormFile objects in the form bean class. loop through them in the action and use the code given by yui0329 to attach them!


- Rakesh
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Attach files to a record or form jetewright Access 2 October 31st, 2008 12:59 PM
jsp files and html files jamara ASP.NET 2.0 Basics 1 November 20th, 2006 07:03 AM
How to Attach movie files kumarj Beginning PHP 0 October 4th, 2006 06:33 AM
Attach E-mail in CDONTS jmss66 Classic ASP Basics 8 March 25th, 2004 09:18 PM



All times are GMT -4. The time now is 08:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.