Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Java Email More...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 16074 of 16268
Post > Topic >>

Java Email More then one attachment.

by ChinoKhan <kalimkhan@[EMAIL PROTECTED] > May 12, 2008 at 09:35 AM

/*
 * Email.java
 *
 * Created on April 11, 2008, 8:08 AM
 */

package util.src;
im****t java.util.*;
im****t java.io.*;
im****t javax.mail.*;
im****t javax.mail.internet.*;
im****t javax.activation.*;


/**
 *
 * @[EMAIL PROTECTED]
  kkhan
 */
public class Email {
    Multipart mp = new MimeMultipart();
    /** Creates a new instance of Email */
    public Email() {
    }

    public void EmailSend(String to,String from,String host,String[]
arrayfilename,String msgText1,String subject){
        // create some properties and get the default Session
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        int i=0;
        int j=0;

        Session mailsession = Session.getInstance(props, null);
        String filename="";

        try {

            for (i=0,  i < arrayfilename.length; i++) {

                System.out.println("Filename "+arrayfilename[i]);

            }

            if(subject == null) {
                subject = "(No Subject)";
            }
            if (msgText1 == null) {
                msgText1 = "";
            }

            // create a message
            MimeMessage msg = new MimeMessage(mailsession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);

            // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(msgText1);


            //MimeBodyPart mbp2 = new MimeBodyPart();
            for (i=0, j=0; i < arrayfilename.length; i++) {

                if(arrayfilename[i]!=null && !
arrayfilename[i].equals("")){
                   MimeBodyPart mbp2 = new MimeBodyPart();
                    // create the second message part
                    //MimeBodyPart mbp2 = new MimeBodyPart();
                    // attach the file to the message
                    FileDataSource fds = new
FileDataSource(arrayfilename[i]);
                    mbp2.setDataHandler(new DataHandler(fds));
                    mbp2.setFileName(fds.getName());
                    mp.addBodyPart(mbp2);
                }

            }
            // create the Multipart and add its parts to it
            //Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            //mp.addBodyPart(mbp2);

            // add the Multipart to the message
            msg.setContent(mp);

            // set the Date: header
            msg.setSentDate(new Date());

            // send the message
            Trans****t.send(msg);

        }
        catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
        }
    }
}
 




 2 Posts in Topic:
Java Email More then one attachment.
ChinoKhan <kalimkhan@[  2008-05-12 09:35:47 
Re: Java Email More then one attachment.
Lothar Kimmeringer <ne  2008-05-12 18:53:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 17:46:33 CDT 2008.