灌灌水,附上我使用javamail smtp的类

发表于:2007-07-04来源:作者:点击数: 标签:
使用范例: /* * new sendmail() 使用GBK的默认编码发送文本信息 * new sendmail(编码,文本还是html); */ wuyu.javamail.sendmail mail = new wuyu.javamail.sendmail( GBK, true ); /* * mail.connect (smtp 服务器 ); * main.connect(smtp服务器,用户名,密
使用范例:
/*
        * new sendmail() 使用GBK的默认编码发送文本信息
        * new sendmail(编码,文本还是html);
        */
        wuyu.javamail.sendmail mail = new wuyu.javamail.sendmail( "GBK", true );
        /*
        * mail.connect (smtp服务器);
        * main.connect(smtp服务器,用户名,密码); 适用于smtp认证的发信服务器
        */
        mail.connect( args[0], args[1], args[2] );
        for ( int i = 0; i < Integer.parseInt( args[3] ); i++ ) {
            System.out.print( i + "\t" );
            /*
            * main.send(from,to,cc,bcc,主题,正文,附件文件名)
            * to,cc,bcc可填写多个mail地址
            * 附件文件名为null,表示不发送附件
            */
            mail.send( "mmwy@ynwst.com", args[4], "", "", "这是主题" + i, "<b>这是正文</b>", null );
            System.out.println( i );
        }
        mail.close();
=-=--------------------

sendmail.java
------------------------------
package wuyu.javamail;

/**
* 调用java mail发送邮件的类
*/
public class sendmail {
    private String encode;//文本编码
    private boolean HTML;//文本还是html内容
    private javax.mail.Session session;
    private javax.mail.Transport transport;

    public sendmail() {
        this.encode = "GBK";
        this.HTML = false;
    }

    public sendmail( String encode, boolean html ) {
        this.encode = encode;
        this.HTML = html;
    }

    public boolean connect( String smtpHost ) {
        return connect( smtpHost, null, null );
    }

    public boolean connect( String smtpHost, String uid, String pwd ) {
        try {
            java.util.Properties properties = new java.util.Properties();
            properties.put( "mail.smtp.host", smtpHost );
            if ( uid == null || uid.equals( "" ) ) {
                session = javax.mail.Session.getInstance( properties, null );
            } else {
                properties.put( "mail.smtp.auth", "true" );
                Auth auth = new Auth( uid, pwd );
                session = javax.mail.Session.getInstance( properties, auth );
            }
            transport = session.getTransport( "smtp" );
            transport.connect();
            return true;
        }
        catch ( Exception e ) {
            e.printStackTrace();
            return false;
        }
    }

    public void close() {
        if ( transport != null ) {
            try {
                transport.close();
            }
            catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }

    public void send( String from, String to, String cc, String bcc, String subject, String text, String[] filename ) {
        try {
            javax.mail.internet.MimeMessage mimeMessage = createMimeMessage( session,
                    from, to, cc, bcc, subject, text, filename );
            transport.sendMessage( mimeMessage, mimeMessage.getAllRecipients() );
        }
        catch ( javax.mail.MessagingException e ) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
    }

    private javax.mail.internet.MimeMessage createMimeMessage(
            javax.mail.Session session,
            String from, String to, String cc, String bcc,
            String subject, String text,
            String[] filename ) throws javax.mail.MessagingException {
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage( session );
        javax.mail.internet.InternetAddress fromAddress = new javax.mail.internet.InternetAddress( from );
        message.setFrom( fromAddress );

        javax.mail.internet.InternetAddress[] toAddress = javax.mail.internet.InternetAddress.parse( to );
        javax.mail.internet.InternetAddress[] ccAddress = javax.mail.internet.InternetAddress.parse( cc );
        javax.mail.internet.InternetAddress[] bccAddress = javax.mail.internet.InternetAddress.parse( bcc );

        message.setRecipients( javax.mail.Message.RecipientType.TO, toAddress );
        message.setRecipients( javax.mail.Message.RecipientType.CC, ccAddress );
        message.setRecipients( javax.mail.Message.RecipientType.BCC, bccAddress );

        message.setSubject( subject, this.encode );
        //处理附件
        if ( filename != null && filename.length > 0 ) {
            javax.mail.Multipart multipart = new javax.mail.internet.MimeMultipart();

            javax.mail.internet.MimeBodyPart mimeBodyPart = new javax.mail.internet.MimeBodyPart();
            if ( HTML ) {
                mimeBodyPart.setContent( text, "text/html;charset="+this.encode );
            } else {
                mimeBodyPart.setText( text, this.encode );
            }
            multipart.addBodyPart( mimeBodyPart );
            for ( int i = 0; i < filename.length; i++ ) {
                try {
                    javax.mail.internet.MimeBodyPart fileBodyPart = new javax.mail.internet.MimeBodyPart();
                    javax.activation.DataSource datasource = new javax.activation.FileDataSource( filename[i] );
                    fileBodyPart.setDataHandler( new javax.activation.DataHandler( datasource ) );
                    fileBodyPart.setFileName( new java.io.File( filename[i] ).getName() );
                    multipart.addBodyPart( fileBodyPart );
                }
                catch ( Exception e ) {
                    e.printStackTrace();
                }
            }
            message.setContent( multipart );
        } else {
            if ( HTML ) {
                message.setContent( text, "text/html;charset="+this.encode );
            } else {
                message.setText( text, this.encode );
            }
        }
        return message;
    }
}




Auth.java
------------------------
package wuyu.javamail;


public class Auth extends javax.mail.Authenticator {
    private String user,pwd;

    public Auth( String user, String pwd ) {
        this.user = user;
        this.pwd = pwd;
    }

    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
        return new javax.mail.PasswordAuthentication( this.user, this.pwd );
    }
}

原文转自:http://www.ltesting.net