2010年9月15日 星期三

Sending email with attachments via a SSL smtp server by java

1. First you need to download the library "javamail". Please download and extract it. There should be a file "mail.jar" in it. Setting the environment variable CLASSPATH to $CLASSPATH:/your_dir/mail.jar

2. You may(or may not) need to download the certification of your smtp and put it to the trust list. Please refer to this page http://blogs.sun.com/andreas/entry/no_more_unable_to_find . And get the InstallCert.java here: http://blogs.sun.com/andreas/resource/InstallCert.java

3. Refering to the program shown below:

====
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.security.*;

public class SendFileMail {
    public static void main (String [] args) {
        // some parameters
        String to = "the target";
        String from = "your email address";
     final String username = "your email address on the smtp server";
   final String pass = "your password of your smtp server";
        String host = "your smtp server name";
     int port = 465; // for SSL, it is usually 465
      
        // SSL setting
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  
        // setting the certification
        // only need this line for adding the certification of your smtp server to the trust list
     System.setProperty("javax.net.ssl.trustStore", "jssecacerts");

        // setting the properties
        Properties properties = System.getProperties();
     properties.setProperty("mail.smtp.host", host);
   properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
   properties.setProperty("mail.smtp.socketFactory.fallback", "false");
   properties.setProperty("mail.smtp.port", Integer.toString(port));
   properties.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));
     properties.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(properties,
     new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication () {
    return new PasswordAuthentication(username, pass);
    }
    });

        // create the message
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("The Subject");
            Multipart multipart = new MimeMultipart();
            // adding the text message
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("text body");
            multipart.addBodyPart(messageBodyPart);
            // adding a file
            messageBodyPart = new MimeBodyPart();
            String file_name = "the file name";
            DataSource source = new FileDataSource(file_name);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(file_name);
            multipart.addBodyPart(messageBodyPart);
            // send message
            message.setContent(multipart);
            Transport.send(message);
        } catch (MessagingException mex) {
            mex.printStackTrace();          
        }
    }
}
====