Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

Java Email More then one attachment.

Thread view: 
ChinoKhan - 12 May 2008 17:35 GMT
/*
* Email.java
*
* Created on April 11, 2008, 8:08 AM
*/

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

/**
*
* @author  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
           Transport.send(msg);

       }
       catch (MessagingException mex) {
           mex.printStackTrace();
           Exception ex = null;
           if ((ex = mex.getNextException()) != null) {
               ex.printStackTrace();
           }
       }
   }
}
Lothar Kimmeringer - 12 May 2008 17:53 GMT
[Source]

Your question was?

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.