Results 1 to 2 of 2

Thread: JavaMail API

  1. #1
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default JavaMail API

    i am trying to send an email from java using the JavaMail API however i am getting an error...

    Access restriction: The type FileDataSource is not accessible due to restriction on required library C:\Users\Eduardo\AppData\Local\Genuitec\Common\bin ary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\r t.jar
    the error comes from this line...
    Java Code:
                
     //use a JAF FileDataSource as it does MIME type detection
    DataSource source = new FileDataSource(filename);
    attachmentBodyPart.setDataHandler(new DataHandler(source));
    this is the code i got from the internet..
    Java Code:
     import javax.mail.*;
     import javax.mail.internet.*;
     import javax.activation.*;
     import java.io.*;
     import java.util.Properties;
     public class SendMail{
     
         public void sendMail(String mailServer, String from, String to,String subject, String messageBody,
                                 String[] attachments) throws MessagingException, AddressException {
             // Setup mail server
             Properties props = System.getProperties();
             props.put("mail.smtp.host", mailServer);
             
             // Get a mail session
             Session session = Session.getDefaultInstance(props, null);
             
             // Define a new mail message
             Message message = new MimeMessage(session);
             message.setFrom(new InternetAddress(from));
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
             message.setSubject(subject);
             
             // Create a message part to represent the body text
             BodyPart messageBodyPart = new MimeBodyPart();
             messageBodyPart.setText(messageBody);
             
             //use a MimeMultipart as we need to handle the file attachments
             Multipart multipart = new MimeMultipart();
             
             //add the message body to the mime message
             multipart.addBodyPart(messageBodyPart);
             
             // add any file attachments to the message
             addAtachments(attachments, multipart);
             
             // Put all message parts in the message
             message.setContent(multipart);
             
             // Send the message
             Transport.send(message);
     
     
         }
     
         protected void addAtachments(String[] attachments, Multipart multipart)
                         throws MessagingException, AddressException
         {
             for(int i = 0; i<= attachments.length -1; i++)
             {
                 String filename = attachments[i];
                 MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                 
                 //use a JAF FileDataSource as it does MIME type detection
                 DataSource source = new FileDataSource(filename);
                 attachmentBodyPart.setDataHandler(new DataHandler(source));
                 
                 //assume that the filename you want to send is the same as the
                 //actual file name - could alter this to remove the file path
                 attachmentBodyPart.setFileName(filename);
                 
                 //add the attachment
                 multipart.addBodyPart(attachmentBodyPart);
             }
         }
    }

  2. #2
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Javamail
    By johniem in forum New To Java
    Replies: 1
    Last Post: 01-29-2010, 04:24 PM
  2. JavaMail Jar
    By rummy in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2010, 04:51 PM
  3. Javamail
    By v_mallikarjun in forum Advanced Java
    Replies: 14
    Last Post: 04-18-2008, 08:32 PM
  4. Using JavaMail API
    By consult4u in forum Networking
    Replies: 0
    Last Post: 08-09-2007, 11:15 AM
  5. This is Regarding JSP with javamail
    By venkatkomalli in forum Advanced Java
    Replies: 3
    Last Post: 07-19-2007, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •