Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

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
Avatar of kawas
kawas
Flag of United States of America image

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 );
  }
}
Avatar of princehyderabad
princehyderabad

ASKER

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
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
}

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.
Atleast for testing that code let me know how to attached file
ASKER CERTIFIED SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
yep, i forgot to say that. sorry
Avatar of Mayank S
You can also make it a static method, like a utility:

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

And call it using:

JavaMail.sendMail ( .... ) ;