java发送邮件

首先导入 mail.jar


public class SendEmail {

    public void sendMail(String to,String text){

        //创建连接对象,连接到邮件服务器
        Properties properties = new Properties();

        //设置发送邮件的基本参数
        properties.put("mail.transport.protocol","smtp");//连接协议
        properties.put("mail.smtp.host","smtp.qq.com");//主机名
        properties.put("mail.smtp.port","465");//端口号
        properties.put("mail.smtp.auth","true");
        properties.put("mail.smtp.ssl.enable","true");//设置是否使用ssl连接

//        properties.put("mail.debug","true");//设置是否显示debug信息,true会在控制台显示

        //得到回话对象
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("发件人QQ邮箱","发件人QQ邮箱密码");
            }
        });

        //创建邮箱对象
        Message message = new MimeMessage(session);
        //设置发件人
        try {
            message.setFrom(new InternetAddress("发件人邮箱"));
            //设置收件人
            message.setRecipient(RecipientType.TO,new InternetAddress(to));
            //设置邮件标题
            message.setSubject("注册邮件验证");
            //设置时间
            message.setSentDate(new Date());
            //设置邮件内容
            message.setContent(text,"text/html;charset=UTF-8");
            //得到邮差对象
            Transport transport = session.getTransport();
            //连接到自己的邮箱账户
            transport.connect("发件人邮箱","发件人邮箱密码(不同于普通的登录密码)");
            //发送邮件
            transport.sendMessage(message,message.getAllRecipients());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

参考链接:http://blog.csdn.net/kenhins/article/details/17588019

  http://www.cnblogs.com/SamGroves/p/7231024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值