Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Java
 

Send Mail using mail.jar and activation.jar


Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 9th Nov 2006   Read: 8932 times  

Filed in: Java
Add Comment


 


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 Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





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

Popular Coders

# Coder NameHits
1. iamdvr35752
2. venki_madesh30853
3. srinivasaraobora22477
4. bora_srinivasarao11728
5. Kiran.jakkaraju4609
6. parmod kumar duhan4502
7. chowsys3970
8. Venkateswara Rao3713
9. Raju3390
10. Vamshidhar Matam2290

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape