一个发送mail的javabean

2008-02-23 07:43:22来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  /*
*Author:tyfun
*DateTime:2003.01.09
*Package:com.westarsoft.function
*/

package com.westarsoft.function;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
  private String SMTPServer = new String();
  private String from = new String();
  private String subject = new String();
  private String content = new String();
  private String user = new String();
  private String password = new String();
  public String getSMTPServer() {
    return SMTPServer;
  }
  public void setSMTPServer(String SMTPServer) {
    this.SMTPServer = SMTPServer;
  }
  public String getFrom() {
    return from;
  }
  public void setFrom(String from) {
    this.from = from;
  }
  public String getSubject() {
    return subject;
  }
  public void setSubject(String subject) {
    this.subject = subject;
  }
  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
  public String getUser() {
    return user;
  }
  public void setUser(String user) {
    this.user = user;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }

  public void sendMailTo(String to) {
    try {
      Properties props = new Properties();
      Session sendMailSession;
      Store store;
      Transport transport;
      
      sendMailSession = Session.getInstance(props, null);
      props.put("mail.smtp.host", SMTPServer);
      MimeMessage newMessage = new MimeMessage(sendMailSession);
      newMessage.setFrom(new InternetAddress(from));
       newMessage.setSubject(subject);
      newMessage.setSentDate(new Date());
      newMessage.setText(content);

      StringTokenizer token = new StringTokenizer(to, ",");    
      InternetAddress[] addrArr = new InternetAddress[token.countTokens()];
      int i = 0;
      while(token.hasMoreTokens()) {
        addrArr[i] = new InternetAddress(token.nextToken().toString());
        i ;
      }

      transport = sendMailSession.getTransport("smtp");
      transport.connect(SMTPServer, user, password);
      newMessage.setRecipients(Message.RecipientType.TO, addrArr);
      transport.send(newMessage);
    }
    catch(Exception e) {
      System.out.println(e);
    }
  }
  
  /*
  public static void main(String[] args) {
    SendMail mail = new SendMail();
    mail.setSMTPServer("200.1.1.157");
    mail.setUser("lint");
    mail.setPassword("30320");
    mail.setFrom("lint@westar.com");
    mail.setSubject("测试");
    mail.setContent("test");
    mail.sendMailTo("lint@westar.com,zouy@westar.com,zhangp@westar.com");
  }
  */
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在Jsp程序读取或向DB写入数据乱码解决办法

下一篇:一个向数据库存取image文件的jsp程序