一个java里用来发邮件的类

一个java里用来发邮件的类
网上找得,觉得还不错。不过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();
    }
}

你可能感兴趣的:(一个java里用来发邮件的类)