zwleisa

语源科技BlogJava 首页 新随笔 联系 聚合 管理
  18 Posts :: 0 Stories :: 10 Comments :: 0 Trackbacks
网上找得,觉得还不错。不过MS看到spring里也有类似的功能,有空的话可以研究一下。
/**
 * 文件名称:EMailTool
 * 文件描述:

 × 产品标识:
 × 单元标识:

 × 编写人: zhang wei
 * 编写时间: 2007-2-25
 
*/

import javax.mail.*;
import javax.mail.Store;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;


/**
 * todo:
 *
 * 
@author zhang wei
 * 
@version 1.0
 
*/

public class EMailTool
{
    
private String from;
    
private String to;
    
private String user;
    
private String password;
    
private String smtpHost;
    
private String pop3Host;
    
private String hostName;

    
public String getFrom()
    {
        
return from;
    }

    
public void setFrom( String from )
    {
        
this.from = from;
    }

    
public String getTo()
    {
        
return to;
    }

    
public void setTo( String to )
    {
        
this.to = to;
    }

    
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 String getSmtpHost()
    {
        
return smtpHost;
    }

    
public void setSmtpHost( String smtpHost )
    {
        
this.smtpHost = smtpHost;
    }

    
public String getPop3Host()
    {
        
return pop3Host;
    }

    
public void setPop3Host( String pop3Host )
    {
        
this.pop3Host = pop3Host;
    }

    
public String getHostName()
    {
        
return hostName;
    }

    
public void setHostName( String hostName )
    {
        
this.hostName = hostName;
    }

    
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 getFilename()
    {
        
return filename;
    }

    
public void setFilename( String filename )
    {
        
this.filename = filename;
    }

    
private String subject;
    
private String content;
    
private String filename;

    
private void sendMail() throws MessagingException
    {
        
try
        {
            
// this.smtpHost = this.createSmtpHost();
            
// Get system properties
            
// Properties props = System.getProperties();
            Properties props = new Properties();
            
// Setup mail server
            props.put("mail.smtp.host"this.smtpHost);

            props.put(
"mail.smtp.auth""true"); //验证

            
// Get session
            Session session = Session.getDefaultInstance(props);
            session.setDebug(
true);
            
// Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(
new InternetAddress(from));
            
//message.setFrom();
            message.addRecipient(Message.RecipientType.TO,
                                 
new InternetAddress(to));
            message.setSubject(
this.getSubject());
             System.out.println(
"::"+this.getFilename());
            
if (this.getFilename() != null)
            {
                BodyPart messagebodyPart 
= new MimeBodyPart();
                messagebodyPart.setContent(
this.getContent(),
                                           
"text/html;charset=gb2312");
                System.out.println(
"文件名称:" + this.getFilename());
                Multipart multipart 
= new MimeMultipart();
                multipart.addBodyPart(messagebodyPart);
                messagebodyPart 
= new MimeBodyPart();
                DataSource source 
= new FileDataSource(this.getFilename());
                messagebodyPart.setDataHandler(
new DataHandler(source));
                messagebodyPart.setFileName(
this.getFilename());
                multipart.addBodyPart(messagebodyPart);

                
// message.setContent(this.content, "text/html;charset=gb2312");
                message.setContent(multipart);
                message.setSentDate(
new java.util.Date());

                
// Send message
                message.saveChanges();
                Transport transport 
= session.getTransport("smtp");
                transport.connect(
this.smtpHost, this.user, this.password);

                transport.sendMessage(message,
                                      message.getAllRecipients());
                transport.close();
                System.out.println(
"发送成功!");
            }
            
else
            {
                message.setContent(
this.content, "text/html;charset=gb2312");
                message.setSentDate(
new java.util.Date());
                
// Send message
                message.saveChanges();
                Transport transport 
= session.getTransport("smtp");
                transport.connect(
this.smtpHost, this.user, this.password);
                message.saveChanges();

                
//transport.sendMessage(message, message.getAllRecipients());
                transport.sendMessage(message,
                                      message.getRecipients(Message.
                        RecipientType.TO));
                transport.close();
            }
        }
        
catch (Exception e)
        {
             e.printStackTrace();
             
throw new MessagingException( e.toString() );
        }
    }

    
//sendMail start method
    public void startSend() throws MessagingException {
        sendMail();
    }
}
posted on 2007-04-04 18:04 神仙叹 阅读(225) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: