Results 1 to 2 of 2
Thread: JavaMail API
- 02-28-2010, 12:12 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
JavaMail API
i am trying to send an email from java using the JavaMail API however i am getting an error...
the error comes from this line...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
this is the code i got from the internet..Java Code://use a JAF FileDataSource as it does MIME type detection DataSource source = new FileDataSource(filename); attachmentBodyPart.setDataHandler(new DataHandler(source));
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); } } }
- 02-28-2010, 12:39 AM #2
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Javamail
By johniem in forum New To JavaReplies: 1Last Post: 01-29-2010, 03:24 PM -
JavaMail Jar
By rummy in forum Advanced JavaReplies: 1Last Post: 01-21-2010, 03:51 PM -
Javamail
By v_mallikarjun in forum Advanced JavaReplies: 14Last Post: 04-18-2008, 07:32 PM -
Using JavaMail API
By consult4u in forum NetworkingReplies: 0Last Post: 08-09-2007, 10:15 AM -
This is Regarding JSP with javamail
By venkatkomalli in forum Advanced JavaReplies: 3Last Post: 07-19-2007, 02:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks