Java Mail Attachment

hi experts,

I'v this code works fine now I need to modify this in order to make attachment file work:

ublic class JavaMail {          
      
      public String sendMail(java.lang.String  from, java.lang.String to)
                    throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
                    {                
          try {
                String textmessage = "some msg....";
                        
                InitialContext  ctx = new InitialContext();
                   Session mailSession = (Session)  ctx.lookup("java:comp/env/mail/xz");
                  Message message = new MimeMessage(mailSession);
                  
                  //Set  the from address
                   InternetAddress[] fromAddress = InternetAddress.parse(from);
                   message.addFrom(fromAddress);

                //Set  the to address                   
                   InternetAddress[] toAddress = InternetAddress.parse(to);
                   message.setRecipients(Message.RecipientType.TO,toAddress);

                  //Set  the subject and text
                    message.setSubject(subject);
                    message.setText(textmessage);

                        //send mail
                     Transport.send(message);      
                }           
      
       catch(AddressException  e) {
             status = "Address Exception: "+e;
            System.out.println(status);
      } catch(SendFailedException  e) {
            status = "Send Failed Exception: "+e;
            System.out.println(status);
      } catch  (MessagingException e) {
            status = "MessagingException: "+e;
            System.out.println(status);
      } catch  (NamingException e) {
            status = "Naming Exception: "+e;
            System.out.println(status);
      } catch  (Exception e) {
            status = "General Exception: "+e;
            System.out.println(status);            
      }      
      return status;

                    } // END sendMail
}

and the way I'm calling above class in another class:
..
 JavaMail rm = new JavaMail();
 jm.sendMail(FromEmailAddress, ToEmailAddress);
..

Please let me what changes do I need to go for
thx,
PH
princehyderabadAsked:
Who is Participating?
 
kawasCommented:
public class JavaMail {          
     
     public String sendMail(java.lang.String  from, java.lang.String to, java.io.File file)
                 throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
                 {              
         try {
              String textmessage = "some msg....";
                     
              InitialContext  ctx = new InitialContext();
                Session mailSession = (Session)  ctx.lookup("java:comp/env/mail/xz");
               Message message = new MimeMessage(mailSession);
               
               //Set  the from address
                InternetAddress[] fromAddress = InternetAddress.parse(from);
                message.addFrom(fromAddress);

              //Set  the to address                
                InternetAddress[] toAddress = InternetAddress.parse(to);
                message.setRecipients(Message.RecipientType.TO,toAddress);

               //Set  the subject and text
                 message.setSubject(subject);
                 message.setText(textmessage);


    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source =
      new FileDataSource(file);
    messageBodyPart.setDataHandler(
      new DataHandler(source));
    messageBodyPart.setFileName(file.getName());
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

                   //send mail
                  Transport.send(message);    
              }          
     
      catch(AddressException  e) {
           status = "Address Exception: "+e;
          System.out.println(status);
     } catch(SendFailedException  e) {
          status = "Send Failed Exception: "+e;
          System.out.println(status);
     } catch  (MessagingException e) {
          status = "MessagingException: "+e;
          System.out.println(status);
     } catch  (NamingException e) {
          status = "Naming Exception: "+e;
          System.out.println(status);
     } catch  (Exception e) {
          status = "General Exception: "+e;
          System.out.println(status);          
     }    
     return status;

                 } // END sendMail
}
0
 
kawasCommented:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class AttachExample {
  public static void main (String args[])
      throws Exception {
    String host = args[0];
    String from = args[1];
    String to = args[2];
    String fileAttachment = args[3];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session =
      Session.getInstance(props, null);

    // Define message
    MimeMessage message =
      new MimeMessage(session);
    message.setFrom(
      new InternetAddress(from));
    message.addRecipient(
      Message.RecipientType.TO,
      new InternetAddress(to));
    message.setSubject(
      "Hello JavaMail Attachment");

    // create the message part
    MimeBodyPart messageBodyPart =
      new MimeBodyPart();

    //fill message
    messageBodyPart.setText("Hi");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source =
      new FileDataSource(fileAttachment);
    messageBodyPart.setDataHandler(
      new DataHandler(source));
    messageBodyPart.setFileName(fileAttachment);
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

    // Send the message
    Transport.send( message );
  }
}
0
 
princehyderabadAuthor Commented:
hi kawas

You just copied from :http://www.jguru.com/faq/view.jsp?EID=30251
I already knew this.

What I'm looking is how to modify my existing code so that attachment code can be added. And how to make call to my JavaMail class when attachment code is ready. See my 2nd half question the way I'm calling my javamail class without attchment code. Same way I want to know how to make call to Javamail class when attachment code is updated.

THX,
PH
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kawasCommented:
public class JavaMail {          
     
     public String sendMail(java.lang.String  from, java.lang.String to)
                 throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
                 {              
         try {
              String textmessage = "some msg....";
                     
              InitialContext  ctx = new InitialContext();
                Session mailSession = (Session)  ctx.lookup("java:comp/env/mail/xz");
               Message message = new MimeMessage(mailSession);
               
               //Set  the from address
                InternetAddress[] fromAddress = InternetAddress.parse(from);
                message.addFrom(fromAddress);

              //Set  the to address                
                InternetAddress[] toAddress = InternetAddress.parse(to);
                message.setRecipients(Message.RecipientType.TO,toAddress);

               //Set  the subject and text
                 message.setSubject(subject);
                 message.setText(textmessage);


    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source =
      new FileDataSource(fileAttachment);
    messageBodyPart.setDataHandler(
      new DataHandler(source));
    messageBodyPart.setFileName(fileAttachment);
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

                   //send mail
                  Transport.send(message);    
              }          
     
      catch(AddressException  e) {
           status = "Address Exception: "+e;
          System.out.println(status);
     } catch(SendFailedException  e) {
          status = "Send Failed Exception: "+e;
          System.out.println(status);
     } catch  (MessagingException e) {
          status = "MessagingException: "+e;
          System.out.println(status);
     } catch  (NamingException e) {
          status = "Naming Exception: "+e;
          System.out.println(status);
     } catch  (Exception e) {
          status = "General Exception: "+e;
          System.out.println(status);          
     }    
     return status;

                 } // END sendMail
}

0
 
princehyderabadAuthor Commented:
thanks kawas, I think I'm asking basci stuff.

By the way can U also tell me how to call this javamail. My previous code is called in this way:

JavaMail rm = new JavaMail();
 jm.sendMail(FromEmailAddress, ToEmailAddress);

Now that attachment is also need to pass here, how can i make work.
0
 
princehyderabadAuthor Commented:
Atleast for testing that code let me know how to attached file
0
 
princehyderabadAuthor Commented:
so bascially this is the way I should call my javamail class from other jsp or class as:

JavaMail rm = new JavaMail();
 jm.sendMail(FromEmailAddress, ToEmailAddress, fileAttachment);
0
 
kawasCommented:
yep, i forgot to say that. sorry
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can also make it a static method, like a utility:

public static void sendMail ( .... )
{
}

And call it using:

JavaMail.sendMail ( .... ) ;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.