使用Java发送邮件

源码下载:http://download.csdn.net/detail/qq_35318576/9826213

1.导入相应的jar包:activation.jar 和 mail.jar。(仅供参考的下载地址:http://download.csdn.net/detail/dbhunter/398258)

2.发送邮件代码

package com.yjq.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {

    //发件人的邮箱和密码
    public static String myEmailAccount = "156xxx@163.com";
    public static String myEmailPassword = "123456";
    
    //发件人的SMTP服务器地址
    public static String myEmailSMTPHost = "smtp.163.com";
    
    //收件人邮箱
    public static String receiveEmailAccount = "14xxx@qq.com";
    
    public static void main(String[] args) throws Exception {
        
        //1.创建参数配置,用于连接邮件服务器的参数配置
        Properties props = new Properties();
        //参数配置
        props.setProperty("mail.transport.protocol", "smtp"); //使用的协议(JavaMail规范要求)
        props.setProperty("mail.host", myEmailSMTPHost);  //发件人的邮箱的SMTP服务器地址
        props.setProperty("mail.smtp.auth", "true");  //请求认证,参数名称与具体实现有关
        
        //2.根据配置创建会话对象,用于和邮件服务器交互
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);  //设置debug模式,可以查看详细的发送日志
        
        //3.创建一封邮件
        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveEmailAccount);
        
        //4.根据Session获取邮件传输对象
        Transport transport = session.getTransport();
        
        //5.使用邮箱帐号和密码连接邮件服务器
        transport.connect(myEmailAccount, myEmailPassword);
        
        //6.发送邮件,发到所有的收件地址
        //message.getAllRecipients()获取到的是在创建邮件对象时添加的所有收件人,抄送人,密送人
        transport.sendMessage(message, message.getAllRecipients());
        
        System.err.println("发送邮件成功!");
        
        //7.关闭连接
        transport.close();
        
    }

    /**
     * 创建一封只包含文本的简单邮件
     * @param session 和服务器交互的会话
     * @param myEmailAccount 发件人邮箱
     * @param receiveEmailAccount 收件人邮箱
     * @return
     * @throws MessagingException 
     * @throws Exception 
     */
    private static MimeMessage createMimeMessage(Session session, String myEmailAccount, String receiveEmailAccount) throws Exception {
        
        //1.创建一封邮件
        MimeMessage message = new MimeMessage(session);
        
        //2.From 发件人
        message.setFrom(new InternetAddress(myEmailAccount, "Yuxx", "UTF-8"));
        
        //3.To 收件人
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveEmailAccount, "Hello," + receiveEmailAccount, "UTF-8"));
        
        //4.subject 邮件主题
        message.setSubject("忘记密码", "UTF-8");
        
        //5.content 邮件正文
        message.setContent("忘记了密码怎么办?点击这里找回您的密码。", "text/html;charset=UTF-8");
        
        //6.设置发件时间
        message.setSentDate(new Date());
        
        //7.保存设置
        message.saveChanges();
        
        return message;
    }
}
View Code

 

posted @ 2017-04-12 19:46  走在一线的码农  阅读(588)  评论(0编辑  收藏  举报