码迷,mamicode.com
首页 > 编程语言 > 详细

java邮件

时间:2014-09-17 18:24:22      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   使用   java   ar   div   sp   log   

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();
        }
    }
}

 

java邮件

标签:blog   io   os   使用   java   ar   div   sp   log   

原文地址:http://www.cnblogs.com/sand-tiny/p/3977555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!