java发送邮件

java发送邮件

java能实现的功能很多,这边文章主要介绍如何使用 mail.jar 和 activation.jar 来发送邮件。

首先下载 mail.jar activation.jar,下载链接:
Download mail.jar
Download activation-1.1.0.jar
下载后,解压并添加到 eclipse 中,

为了更方便使用,这里通过实现一个调用类 Mail,调用了在引入的类库中的方法,类的实现如下,包括发送等方法:


public class Mail {
    private String subject;
    private String from;
    private String[] recipients;
    private String message;

    private static final String SMTP_HOST_NAME = "";//你的smtp服务器地址
    private static final String SMTP_AUTH_USER = "";//smtp用户名
    private static final String SMTP_AUTH_PWD  = "";    //密码;
    
    public void send() throws MessagingException{
        boolean debug = false;

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, new SMTPAuthenticator());
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        msg.setHeader("X-Priority", "3");    //1:紧急 3:普通 5:缓慢
        msg.setSubject(subject);
        msg.setContent(message, "text/plain; charset=UTF-8");
        Transport.send(msg);
    }
    
    //如需认证发件,可打开下面注释
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {

        public PasswordAuthentication getPasswordAuthentication()
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
    
    public String getSubject() {
        return subject;
    }


    public void setSubject(String subject) {
        this.subject = subject;
    }


    public String getFrom() {
        return from;
    }


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


    public String[] getRecipients() {
        return recipients;
    }


    public void setRecipients(String[] recipients) {
        this.recipients = recipients;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }



}

需要发送邮件时,直接引用 Mail 类中的方法即可:

private static void sendRenew(String toMail,String message){
        Mail mail = new Mail();
        mail.setFrom("License Manager <huang@163.com>");//发送方
        mail.setRecipients(new String[]{toMail});//接收方,可以多个
        mail.setSubject("Renew Now!!");
        mail.setMessage(message);
        try {
            mail.send();//发送
        } catch (Exception e) {
            System.out.println("error report failure.\n"+e);
        }
}

注意:不过如果你使用的是 MyEclipse 有时候会出现这样的异常

java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

原因:
MyEclipse 中自带的 mail 和 activation 和你引入的 jar 包发生冲突

解决方法:
进入 MyEclipse 的安装目录下
D:\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5

(蓝色部分不同版本略有不同)
可以看到javaee.jar包,

使用压缩软件打开把这个包文件,然后进到javax文件夹中,删除 mail 目录和activation 文件夹, 问题得以解决。

参考:http://blog.sina.com.cn/s/blog_4550f3ca01019qpt.html

 推荐内容:
Gmail 535-5.7.8 Username and Password not accepted