java邮件

package com.test.mail;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Administrator on 14-9-17.
 */
public class JaveMail {
    public static void main(String[] args) throws Exception {
        final String user = "lxb@goutrip.com";
        final String password = "Sand_Tiny@163.com";

        String fromAddress = "lxb@goutrip.com";
        String toAddress = "sand_tiny@163.com";
        String subject = "邮件测试主题";
        String content = "这是一个测试邮件<b>哈哈</b>";

        //配置参数
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.exmail.qq.com");
        // 方法一:使用transport对象发送邮件
        {
            //通过参数生成会话
            Session session = Session.getInstance(props);
            //启用调试模式
            session.setDebug(true);
            //创建一封邮件,并设置信息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromAddress));
            message.setSubject(subject);
            message.setText(content);
            //创建传输
            Transport transport = session.getTransport();
            //连接smtp服务器
            transport.connect(user, password);
            //发送
            transport.sendMessage(message, new InternetAddress[]{new InternetAddress(toAddress)});
            transport.close();
        }
    }
}

 

posted @ 2014-09-17 17:02  逍遥_时空  阅读(181)  评论(0编辑  收藏  举报