// java mail package
http://java.sun.com/products/javamail/index.html
//java mail depends on
http://java.sun.com/products/javabeans/glasgow/jaf.html
//Set your CLASSPATH to include the "mail.jar" file obtained from
// the download, as well as the current directory.
// Assuming you unzipped javamail-1_2.zip in c:\download the
// following would work:
// set CLASSPATH=%CLASSPATH%;c:\download\javamail-1.2\mail.jar;.
// Also include the "activation.jar" file that you obtained from
// downloading the JavaBeans Activation Framework, in your CLASSPATH.
// set CLASSPATH=%CLASSPATH%;c:\download\activation\activation.jar
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mail extends JFrame
{
JLabel jlSendMail = new JLabel("Send Mail");
public Mail()
{
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(jlSendMail);
jlSendMail.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
try{
String host = "smtp.21cn.com";//"Mail Server in your Company";
String from = "
aaaa@21cn.com";//"From Email";
String to = "
bbbb@21cn.com";//"To Email";
Properties props = new Properties();
Session session;
Transport transport;
props.put("mail.smtp.host", host);
session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from,"Ahmad"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to,"Ahmadwa"));
message.setSubject("First");
//message.setSentDate(new Date());
message.setText("Hi Emad" + (char)(10) + "How are you? This is a test mail send by java program"+new Date());
transport = session.getTransport("smtp");
transport.send(message);
System.out.println("Your Mail Send Successfully");
}
catch (MessagingException e) {System.out.println("1)" + e.toString());}
catch (UnsupportedEncodingException e) {System.out.println("2)" + e.toString());}
}
});
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String args[])
{
new Mail();
}
}