邮件发送程序源代码

在eclipse下新建一个project,将下列代码拷贝过去,把MailKit提到的邮件服务器和邮件接收者改写一下即可试用,很久以前写的,拿来分享一下,不知各位是否有用

 

package mail;

import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author  selectme_2008
 */
public class SMTPAuthenticator extends Authenticator
{
    private PasswordAuthentication password_auth;
    
    public SMTPAuthenticator(String smtp_user, String smtp_password)
    {
        password_auth = new PasswordAuthentication(smtp_user, smtp_password);
    }
    
    public PasswordAuthentication getPasswordAuthentication()
    {
        return password_auth;
    }
}

 

//----------------MailKit :主程序----------------

 


package mail;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javatip.SMTPAuthenticator;
import javax.activation.FileDataSource;

/**
 *
 * @author  selectme_2008
 */
public class MailKit {
   
    private String host;
    private String userMail;
    private String pwd;
   
   
    /** Creates a new instance of MailKit
     *@param mailHost: 邮件服务器,如 smtp.163.com
     *@param userAccount: 用户, 如 admin
     *@param password: 登陆密码,如 123456
     */
    private MailKit(String mailHost,String userMail, String password) {
        init(mailHost,userMail,password);
    }
   
    /** 初始化邮件服务器
     *@param mailHost: 邮件服务器,如 smtp.163.com
     *@param userAccount: 用户, 如 admin
     *@param pwd: 登陆密码,如 123456
     */
    private void init(String mailHost,String userMail, String password) {
        if(mailHost == null || userMail == null || password == null) {
            throw new IllegalArgumentException("传人的参数不能为空");
        }
        this.host = mailHost;
        this.userMail = userMail;
        this.pwd = password;
    }
   
   
    /** 发送邮件
     * @param to 邮件接受者,可以有多个接受者 
     * @param subject 邮件主题
     * @param msgContent 邮件正文
     * @param attachFile 邮件附件,可以有多个附件;
     *                   传入的参数为文件数组,文件必须为完整路径名 c:\\viewthread.jsp
     *                   null表示没有附件 
     * @return 发送成功返回true,否则返回false
     */
    public boolean sendMail(String[] to, String subject,String msgContent,String[] attachFile) {
        try {
            java.util.Properties props = new java.util.Properties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth","true");
            props.put("username",userMail);
            props.put("password",pwd);

            Authenticator auth = new SMTPAuthenticator(userMail, pwd);
            Session sendMailSession = Session.getDefaultInstance(props, auth);
            Message msg = new MimeMessage(sendMailSession);
            msg.setFrom(new InternetAddress(userMail));
            if(to != null && to.length > 0) {
                InternetAddress[] address = new InternetAddress[to.length];
               
                for(int i=0; i<to.length; i++) {
                    address[i] = new InternetAddress(to[i]);
                }
                msg.setRecipients(Message.RecipientType.TO, address);
            }
            else {
                return false;
            }
            msg.setSubject(subject);
           
            // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(msgContent);
           
            // create the Multipart and add its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
           
            // create the second message part
            if(attachFile != null && attachFile.length >0) {
                MimeBodyPart mbp2;
                for(int i=0; i<attachFile.length; i++) {
                    mbp2 = new MimeBodyPart();
                    // attach the file to the message
                    FileDataSource fds = new FileDataSource(attachFile[i]);
                    mbp2.setDataHandler(new DataHandler(fds));
                    mbp2.setFileName(fds.getName());
                    mp.addBodyPart(mbp2);
                }
            }
            // add the Multipart to the message
            msg.setContent(mp);
            // set the Date: header
            msg.setSentDate(new Date());
           
            // send the message
            Transport.send(msg);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    public static void main(String []a)
    {
        MailKit mkit = createJavaSalonMailKit();
        mkit.sendMail(new String[]{"support@xxx.net"},"ttttttttt","http://www.xxx.net",null);
        System.out.println("sOK");
    }
   
    /** 创建163用户的邮件信息,发件人是 xxx@163.com */
    public static MailKit create163MailKit()
    {
        String mailHost = "smtp.163.com";
        String userAccount = "xxx@163.com";
        String password = "123456";
        return new MailKit(mailHost,userAccount,password);
    }
   
    /** 创建JavaSalon用户的邮件信息,发件人是 support@xxx.net */
    public static MailKit createJavaSalonMailKit()
    {
        String mailHost = "mail.xxx.net";
        String userAccount = "support@xxx.net";
        String password = "xxx";
        return new MailKit(mailHost,userAccount,password);
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值