邮件发送

9 篇文章 0 订阅
1 篇文章 0 订阅

所需架包:mail-1.4.7.jar

 

public class EmailUtil {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          
        final Properties propt = new Properties();//得到配置对象
        
        propt.put("mail.smtp.auth", "true"); //确定邮件发送的身份验证
        propt.put("mail.smtp.host", "smtp.qq.com");//设置发送服务器
        propt.put("mail.user", "1474276172@qq.com");//设置qq邮箱发送的账号
        propt.put("mail.pssword", "rvjprvwbpkawiejf");//设置qq邮箱发送的授权码
        
        //对信息进行验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // TODO Auto-generated method stub
                //用户名密码验证
                String userName = propt.getProperty("mail.user");
                String password = propt.getProperty("mail.pssword");
                //验证
                return new PasswordAuthentication(userName, password);
            }
        };
        
        Session session = Session.getInstance(propt, authenticator);//设置授权信息
          
        MimeMessage message = new MimeMessage(session);//写邮件发送的内容
        
        //设置发送
        try {
            InternetAddress from = new InternetAddress(propt.getProperty("mail.user"));
            message.setFrom(from);
            
            //设置标题内容
            message.setSubject("粥粥");
            message.setContent("第五次使用Java技术发送邮件测试————(小凳子收)","text/html;charset=utf-8");
            
            //设置接收人
            InternetAddress to = new InternetAddress("2295713129@qq.com");
            message.setRecipient(RecipientType.TO, to);
            
            //开始发送
            Transport.send(message);
            System.out.println("发送成功!!!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("发送失败!!!");
        }
        
        
    }

    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值