Chinaunix首页 | 论坛 | 博客
  • 博客访问: 284116
  • 博文数量: 82
  • 博客积分: 2607
  • 博客等级: 少校
  • 技术积分: 785
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-14 15:23
文章分类

全部博文(82)

文章存档

2012年(4)

2010年(1)

2009年(2)

2008年(8)

2007年(34)

2006年(33)

我的朋友

分类: Java

2007-09-22 08:38:56

 
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
 /**
  * @param args
  */
 public void postMail( String[] recipients, String subject, String message , String from) throws MessagingException
 {
     boolean debug = false;
      // Set the host smtp address
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.ddd.com");
      props.put("mail.smtp.auth","true");
     // create some properties and get the default Session
     MyAuthenticator auth = new MyAuthenticator("ddd","dd6");
    
//     MyAuthenticator auth = new MyAuthenticator("msmp","123456");
     Session session = Session.getDefaultInstance(props, auth);
     session.setDebug(debug);
     // create a message
     MimeMessage msg = new MimeMessage(session);
    
     // set the from and to address
     InternetAddress addressFrom = new InternetAddress(from);
     msg.setFrom(addressFrom);
//     InternetAddress[] addressTo = new InternetAddress[recipients.length];
//     for (int i = 0; i < recipients.length; i++)
//     {
//         addressTo[i] = new InternetAddress(recipients[i]);
//     }
//     msg.setRecipients(Message.RecipientType.TO, addressTo);
    
   
     // Optional : You can also set your custom headers in the Email if you Want
//     msg.addHeader("MyHeaderName", "myHeaderValue");
     // Setting the Subject and Content Type
    
     Address[] to = new InternetAddress[] {
       new InternetAddress("")
     };
    
     msg.setRecipients(Message.RecipientType.TO, to);
    
     msg.setSubject(subject);
     msg.setContent(message, "text/plain");
    
//     Transport tarnsport = session.getTransport("smtp");
//     tarnsport.sendMessage(msg, msg.getAllRecipients());
//     tarnsport.close();
     Transport.send(msg);
 }
 
 
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  SendMail send = new SendMail();
  String[] recipients = {""};
  try {
   send.postMail(recipients, "mao test", "maojj test smsmp", "");
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
 
 
 
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
 String username, password;
 public MyAuthenticator(String username, String password) {
  this.username = username;
  this.password = password;
 }
 public PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(username, password);
 }
}

阅读(917) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~