JavaMail 使用发信身份验证

JavaMail 使用发信身份验证

做了一个小小的程序, 来进行通过 163.com 发送邮件的工作:

/*
 * @(#)MailSender.java 1.00 2004-8-3
 *
 * Copyright 2004 BeanSoft Studio. All rights reserved.
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*; 

/**
 * MailSender, 发送邮件.
 * 
 * @author BeanSoft
 * @version 1.0 2004-8-3
 */
public class MailSender {
    /** 发信人 */
    private String from;
    /** 收信人 */
    private String to;
    /** 主题 */
    private String subject;
    /** 正文 */
    private String body; 

    private static Properties props = new Properties();
    static {
        try {
            InputStream in = MailSender.class
                    .getResourceAsStream("MailSender.ini");
            props.load(in);
            in.close();
        } catch (Exception ex) {
            System.err.println("无法加载配置文件 MailSender.ini:" + ex.getMessage());
            ex.printStackTrace();
        }
    } 

    public MailSender() { 

    }
    /** 
     * 发送邮件.
     * @return boolean - 发送结果 
     */
    public boolean sendMail() {
        if (getBody() == null || getTo() == null || getFrom() == null
                || getSubject() == null) { return false; }
        //--[ Obtain a session
        try {
            //--[ Set up the default parameters
            //        Properties props = new Properties();
            //        props.put("mail.transport.protocol", "smtp" );
            //        props.put("mail.smtp.host", smtpServer );
            //        props.put("mail.smtp.port", "25" ); 

            //--[ Create the session and create a new mail message
            Session mailSession = Session.getDefaultInstance(props);
            Message msg = new MimeMessage(mailSession); 

            //--[ Set the FROM, TO, DATE and SUBJECT fields
            msg.setFrom(new InternetAddress(getFrom()));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress
                    .parse(getTo()));
            msg.setSentDate(new Date());
            msg.setSubject(getSubject()); 

            //--[ Create the body of the mail
            msg.setText(getBody());
            msg.saveChanges();
            // Using the mail authentication, transport.connect(host, user, password)
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props
                    .getProperty("username"), props.getProperty("password")); 

            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close(); 

//            System.out.println("The email below was sent successfully");
        } catch (Exception e) {
            System.out.println(e);
            //            e.printStackTrace();
            return false;
        }
        return true;
    } 

    /**
     * @return Returns the body.
     */
    public String getBody() {
        return body;
    } 

    /**
     * @param body
     *            The body to set.
     */
    public void setBody(String body) {
        this.body = body;
    } 

    /**
     * @return Returns the from.
     */
    public String getFrom() {
        return from;
    } 

    /**
     * @param from
     *            The from to set.
     */
    public void setFrom(String from) {
        this.from = from;
    } 

    /**
     * @return Returns the subject.
     */
    public String getSubject() {
        return subject;
    } 

    /**
     * @param subject
     *            The subject to set.
     */
    public void setSubject(String subject) {
        this.subject = subject;
    } 

    /**
     * @return Returns the to.
     */
    public String getTo() {
        return to;
    } 

    /**
     * @param to
     *            The to to set.
     */
    public void setTo(String to) {
        this.to = to;
    } 

    public static void main(String[] args) {
        MailSender sender = new MailSender(); 

        sender.setFrom("[email protected]");
        sender.setTo("[email protected]");
        sender.setSubject("测试邮件");
        sender.setBody("测试邮件发送成功!"); 

        System.out.println(sender.sendMail());
    }
} 

配置文件名称为 MailSender.ini, 和类文件放在同一目录下(同一包里), 内容如下:

# SMTP 主机地址
mail.smtp.host = smtp.163.com
# SMTP 帐号用户名
username = mailuser
# SMTP 帐号密码
password = mailpassword 

mail.transport.protocol = smtp
mail.smtp.port = 25
mail.smtp.auth = true 

最后一行指定 SMTP 需要验证.

修改这个配置文件为实际的帐号, 然后运行 java MailSender, 即可发送邮件.

你可能感兴趣的:(javamail)