java mail

摘要:java mail

>>>>>>>>>>>>>>>>>>>>>>>>>>use cht hot>>>>>>>>>>>>>>>>>>>>>>>>>>

public class GmailApp1 {
 
/**
* @param args
* @throws UnsupportedEncodingException 
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String host = "ms.cht.com.tw";  
        int port = 25;  
        final String username = "rickeysu";  
        final String password = "1qaz@WSX";  
  
        Properties props = new Properties();  
        props.put("mail.smtp.host", host);  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.smtp.starttls.enable", "true");  
        props.put("mail.smtp.port", port);  
          
        Session session = Session.getInstance(props, new Authenticator() {  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(username, password);  
            }  
        });  
           
        try {    
       Message message = new MimeMessage(session); 
       
       String encoding = "ISO8859_1";
       InternetAddress[] mailAddrs = new InternetAddress[2];
//        mailAddrs[0] = new InternetAddress("rickeysue@gmail.com","gmail",encoding);
//        mailAddrs[1] = new InternetAddress("rickeysu@cht.com.tw","gmail2",encoding);
       mailAddrs[0] = new InternetAddress("rickeysue@gmail.com");
       mailAddrs[1] = new InternetAddress("rickeysu@cht.com.tw");         
       
       message.setFrom(new InternetAddress("rickeysu@cht.com.tw"));  
       message.setRecipients(Message.RecipientType.TO,   mailAddrs);
       message.setSubject("Testing Subject");  
       message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");  
 
       Transport transport = session.getTransport("smtp");  
       transport.connect(host, port, username, password);  
 
       Transport.send(message);  
 
       System.out.println("Done");  
 
        } catch (MessagingException e) {  
            throw new RuntimeException(e);  
        }  
 
}
 
}
 
 
 
>>>>>>>>>>>>>>>>>>>>>>>>>>gmail host>>>>>>>>>>>>>>>>>>>>>>>>>>
  1. import java.util.Properties;  
  2. import javax.mail.Authenticator;  
  3. import javax.mail.Message;  
  4. import javax.mail.MessagingException;  
  5. import javax.mail.PasswordAuthentication;  
  6. import javax.mail.Session;  
  7. import javax.mail.Transport;  
  8. import javax.mail.internet.InternetAddress;  
  9. import javax.mail.internet.MimeMessage;  
  10.   
  11. public class GmailApp2 {  
  12.   
  13.     public static void main(String[] args) {  
  14.         final String username = "puremonkey2007@gmail.com";  
  15.         final String password = "your password";  
  16.   
  17.         Properties props = new Properties();  
  18.         props.put("mail.smtp.host""smtp.gmail.com");  
  19.         props.put("mail.smtp.socketFactory.port""465");  
  20.         props.put("mail.smtp.socketFactory.class",  
  21.                 "javax.net.ssl.SSLSocketFactory");  
  22.         props.put("mail.smtp.auth""true");  
  23.         props.put("mail.smtp.port""465");  
  24.   
  25.         Session session = Session.getInstance(props, new Authenticator() {  
  26.             protected PasswordAuthentication getPasswordAuthentication() {  
  27.                 return new PasswordAuthentication(username, password);  
  28.             }  
  29.         });  
  30.   
  31.         try {  
  32.   
  33.             Message message = new MimeMessage(session);  
  34.             message.setFrom(new InternetAddress("puremonkey2007@gmail.com.tw"));  
  35.             message.setRecipients(Message.RecipientType.TO, InternetAddress  
  36.                     .parse("john_k_lee@trend.com.tw"));  
  37.             message.setSubject("Testing Subject");  
  38.             message  
  39.                     .setText("Dear Mail Crawler,\n\n No spam to my email, please!");  
  40.   
  41.             Transport.send(message);  
  42.             System.out.println("Done");  
  43.   
  44.         } catch (MessagingException e) {  
  45.             throw new RuntimeException(e);  
  46.         }  
  47.     }  
  48. }