Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Send Mail using mail.jar and activation.jar


Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 9th Nov 2006   Read: 52395 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is easy to use. Compile and Run.

set CLASSPATH=%CLASSPATH%;mail.jar;activaion.jar;.
javac Mail.java
java Mail

 


Sample Code
  1.  
  2. import java.util.Properties;
  3. import java.util.ResourceBundle;
  4.  
  5. import javax.mail.Message;
  6. import javax.mail.Part;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeBodyPart;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeMultipart;
  13.  
  14. public class Mail
  15. {
  16.         /**
  17.          The SMTP Server to send the emails and the user name and password to
  18.          authenticate with the server. All these are read from the mail.properties file.
  19.          */
  20.        
  21.         public static String smtpServer;
  22.         public static String userName;
  23.         public static String password;
  24.        
  25.         static
  26.         {
  27.                 ResourceBundle bundle = ResourceBundle.getBundle("mail");
  28.                 smtpServer = bundle.getString("server");
  29.                 userName = bundle.getString("user");
  30.                 password = bundle.getString("password");                               
  31.         }  
  32.        
  33.         /**
  34.          *
  35.          * @param from
  36.          * @param to Cannot be Null
  37.          * @param subject Cannot be Null
  38.          * @param mailText
  39.          */
  40.         public synchronized boolean sendEmail(String from, String to, String subject, String mailText) throws Exception
  41.         {
  42.                 if((to == null)  && (subject == null))
  43.                         return false;
  44.                
  45.                 if(mailText == null)
  46.                         mailText = "";
  47.  
  48.                 if(from == null)
  49.                         from = userName;
  50.                
  51.                 try {
  52.                         Properties props = System.getProperties();
  53.                         props.put( "mail.smtp.host", smtpServer ) ;
  54.  
  55.                         //SMTP server authentication is set to false, by default. Setting it to true as shown below
  56.                         props.put( "mail.smtp.auth", "true" ) ;
  57.  
  58.                         Session session = Session.getDefaultInstance(props, null);
  59.                         MimeMessage message = new MimeMessage(session);
  60.  
  61.                         //Setting the 'from', 'to', 'cc' addresses and the 'subject'
  62.                         message.setFrom(new InternetAddress(from));
  63.                         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  64.                         message.setSubject(subject);
  65.  
  66.                         //Making the mail body as inline and of html type
  67.                         MimeMultipart mp = new MimeMultipart();
  68.                         MimeBodyPart text = new MimeBodyPart();
  69.                         text.setDisposition(Part.INLINE);
  70.                         text.setContent(mailText, "text/html");
  71.                         mp.addBodyPart(text);
  72.                         message.setContent(mp);
  73.  
  74.                         //SMTP authentication
  75.                         Transport transport = session.getTransport ("smtp") ;
  76.                         transport.connect (smtpServer, userName, password) ;
  77.                         message.saveChanges();
  78.                         transport.sendMessage(message, message.getAllRecipients());
  79.                         transport.close();
  80.                         return true;
  81.                 } catch (Exception e){
  82.                         System.err.println("Email could not be sent due to error: "+e);
  83.                         e.printStackTrace();
  84.                         throw e;                       
  85.                 }
  86.         }
  87.        
  88.         public static void main(String[] args)
  89.         {
  90.                 Mail mail = new Mail();
  91.                 try {
  92.                         mail.sendEmail(null,"[[email id]]","test","I am testing.");
  93.                 } catch ( Exception ex ) {
  94.                         ex.printStackTrace();
  95.                 }
  96.         }      
  97. }
  98.  
Copyright GeekInterview.com


Next Article: Grid Bag Layout


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

where are the twp jar files?
Comment posted by: Yahay on 2007-06-05T16:52:50
Boss, can u paste the properties file, and this 2 jars mail.jar and activaition.jar ,send me the link to be downloaded
Comment posted by: pappu on 2007-08-20T11:03:45

1. www . lib . multiservers . com/mail/mail . properties
2. www . lib . multiservers . com/mail/mail . jar
3. www . lib . multiservers . com/mail/activation . jar


The Jar files are Sun's product and can also be downloaded from
java . sun . com


Best Regards

Comment posted by: Ejaz Azeem on 2007-08-24T05:58:09
"m getting this . can any 1 help me


java.lang.ExceptionInInitializerError
Caused by: java.util.MissingResourceException: Can't find bundle for base name mail, locale en_IN
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1250)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:705)
at Mail.(Mail.java:20)
Caused by: java.lang.NoClassDefFoundError: mail (wrong name: Mail)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2289)
at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1364)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1328)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1282)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1282)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1224)
... 2 more
Could not find the main class: Mail. Program will exit.
Exception in thread "main" Java Result: 1
Comment posted by: praga on 2010-09-18T06:13:35

Popular Coders

# Coder NameHits
1. srinivasaraobora215057
2. iamdvr136011
3. venki_madesh38272
4. Raju30563
5. Kiran.jakkaraju22659
6. Nidhi Saxena22007
7. parmod kumar duhan20501
8. yuvanmytri14861
9. venkat_kiran12052
10. chowsys10964

Active Coders

Refined Tags