Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Simple Java Mail application


Code ResourceAuthor: srinivasaraobora  

Difficulty Level:

Published: 7th Nov 2006   Read: 14747 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is useful for who learn javamail API.

 


Sample Code
  1.  
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import java.net.InetAddress;
  5. import java.util.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.activation.*;
  9.  
  10. class SendMail2 extends Frame implements ActionListener {
  11.  
  12.    
  13.        
  14.     Label l1,l2,l3,l4,l5;
  15.     TextField to,from,subject,attchments,res;
  16.     TextArea content;
  17.     Button b1;
  18.     SendMail2(){
  19.     setLayout(new FlowLayout());
  20.  
  21.         l1 = new Label("To:",Label.RIGHT);
  22.                 l2 = new Label("From:",Label.RIGHT);
  23.         l3= new Label("Subject:",Label.RIGHT);
  24.         l4= new Label("content:",Label.RIGHT);
  25.         l5= new Label("attchments:",Label.RIGHT);
  26.  
  27.             to = new TextField(20);
  28.             from = new TextField(20);
  29.             subject = new TextField(20);
  30.             attchments = new TextField(20);
  31.             content = new TextArea(6,20);
  32.             b1 = new Button("Send Mail");
  33.             res = new TextField(20);
  34.  
  35.                         this.add(l1);
  36.                         this.add(to);
  37.                         this.add(l2);
  38.                         this.add(from);
  39.                         this.add(l3);
  40.                         this.add(subject);
  41.                         this.add(l4);
  42.                         this.add(content);
  43.             this.add(l5);
  44.                         this.add(attchments);
  45.                         this.add(b1);
  46.             this.add(res);
  47.             to.addActionListener(this);
  48.             to.setBackground(new Color(90,45,30));
  49.             to.setForeground(Color.white);
  50.             from.addActionListener(this);
  51.              from.setBackground(new Color(90,45,30));
  52.              from.setForeground(Color.white);
  53.             subject.addActionListener(this);
  54.             subject.setBackground(new Color(90,45,30));
  55.             subject.setForeground(Color.white);
  56.             attchments.addActionListener(this);
  57.             attchments.setBackground(new Color(90,45,30));
  58.             attchments.setForeground(Color.white);
  59.             res.addActionListener(this);
  60.             res.setBackground(new Color(90,45,30));
  61.             res.setForeground(Color.white);
  62.            
  63.         Font f1 = new Font("Sans Serif",Font.BOLD,15);
  64.                 content.setFont(f1);
  65.                 content.setForeground(Color.red);
  66.                 content.setBackground(Color.black);
  67.                     b1.setBackground(Color.green);
  68.             b1.setForeground(Color.red);
  69.             b1.addActionListener(this);
  70.       addWindowListener(new WindowAdapter()
  71.                 {
  72.                         public void windowClosing(WindowEvent we)
  73.                         {
  74.                                 System.exit(0);
  75.                         }
  76.                 });
  77.             }
  78.     public void actionPerformed(ActionEvent ae)
  79.         {
  80.         try{
  81.         String ogms = "localhost";
  82.         Properties props = System.getProperties();
  83.        
  84.         String mailer = "MyMailerProgram";
  85.         props.put("mail.smtp.host", ogms);
  86.         Session session = Session.getDefaultInstance(props, null);
  87.        
  88.         Message msg = new MimeMessage(session);
  89.         msg.setFrom(new InternetAddress(from.getText()));
  90.         msg.setRecipients(Message.RecipientType.TO,
  91.                                         InternetAddress.parse(to.getText(), false));
  92.         msg.setSubject(subject.getText());
  93.         msg.setHeader("X-Mailer", mailer);
  94.         //msg.setText(content.getText());
  95.  
  96.     MimeBodyPart mbp1 = new MimeBodyPart();
  97.         mbp1.setText(content.getText());
  98.         MimeBodyPart mbp2 = new MimeBodyPart();
  99.         FileDataSource fds= new FileDataSource(attchments.getText());
  100.         mbp2.setDataHandler( new DataHandler(fds));
  101.         mbp2.setFileName(attchments.getText());
  102.         MimeMultipart mp;
  103.         mp = new MimeMultipart();
  104.         mp.addBodyPart(mbp1);
  105.         mp.addBodyPart(mbp2);
  106.         msg.setContent(mp);
  107.  
  108.         Transport.send(msg);
  109.         res.setText("You'r Mail Successfully Sent");
  110.          repaint();
  111.         }
  112.         catch(Exception e) {
  113.            
  114.         }
  115. }
  116.        
  117.    
  118.         public void paint(Graphics g)
  119.         {
  120.         setBackground(new Color(50,50,70));
  121.                
  122.         }
  123.  
  124. public static void main(String[] argv)
  125.         throws Exception{
  126.  
  127.                 SendMail2 s = new SendMail2();
  128.                 s.setSize(250,450);
  129.                 s.setTitle("www.srinumail.com");
  130.                 s.show();
  131.     }
  132. }
  133.  
Copyright GeekInterview.com


Next Article: Simple mouse listener application applet


 

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

It is not having enough description to use...I am able to compile by putting classpath as below
set CLASSPATH=%CLASSPATH%;activation.jar;mail.jar

So I am able to compile but...It is not working ...
Require more usage instructions
Comment posted by: Venkateswara Rao on 2006-11-08T00:48:45
i want more information about javamail...............
Comment posted by: anju on 2010-08-31T05:25:14

Popular Coders

# Coder NameHits
1. srinivasaraobora140713
2. iamdvr102875
3. venki_madesh35424
4. Raju20168
5. parmod kumar duhan16408
6. Kiran.jakkaraju15125
7. chowsys7786
8. ashish.cns6882
9. Venkateswara Rao6774
10. venkat_kiran5893

Active Coders

Refined Tags