Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2005, 02:24 AM
Registered User
 
Join Date: Apr 2005
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

 
Old May 31st, 2005, 06:30 AM
Authorized User
 
Join Date: Apr 2005
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();
    }
  }
}
 
Old October 24th, 2007, 12:12 PM
Registered User
 
Join Date: Oct 2007
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
 
Old October 26th, 2007, 12:19 AM
Friend of Wrox
 
Join Date: Mar 2007
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.