javax.mail发送邮件

本文介绍如何使用Java通过QQ邮箱发送邮件,包括配置邮件服务器、设置邮件内容及附件等关键步骤。提供了完整的代码示例,适用于需要通过程序发送邮件的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



总结(QQ邮箱为例):
1.使用第三方邮箱,需在qq邮箱,设置->账户设置授权码,以授权码作为QQ邮箱登录密码。
2.接受服务器类型POP3
 接收邮件服务器:pop.qq.com   端口:995  使用SSL连接服务器:true
 发送邮件服务器:smtp.qq.com  端口:25   使用SSL连接服务器:true
 (pop.qq.com:为个人邮箱;pop.exmail.qq.com:为企业邮箱)
3.properties.setProperty("mail.smtp.starttls.enable", "true");
 必须加上,否则会报错(A secure connection is requiered(such as ssl))

=============================================================

//import javax.mail.*;

public void senderEmailForPhone(TblEmailInfo mailInfo){
  try{
   BASE64Decoder decoder = new BASE64Decoder();
   PasswordAuthenticator authenticator  = new PasswordAuthenticator(mailInfo.getEmail(),mailInfo.getPassword());
   
   //获得邮件会话属性
   Properties properties = new Properties();
   properties.setProperty("mail.smtp.host", mailInfo.getSendHost());//smtp.qq.com
   properties.setProperty("mail.smtp.port", String.valueOf(mailInfo.getSendHostPort()));//25
   properties.setProperty("mail.smtp.auth", "true");
   properties.setProperty("mail.smtp.starttls.enable", "true");
   
   // 创建Session实例对象
   Session session = Session.getInstance(properties, authenticator);
  
   // 创建MimeMessage实例对象
   Message message = new MimeMessage(session);
   
   // 创建邮件发送者地址
            Address from = new InternetAddress(mailInfo.getEmailInfoSendEmail());
            message.setFrom(from);
           
            // 创建邮件的接收者地址,并设置到邮件消息中
            Address[] tos = null;
            String[] receivers = mailInfo.getEmailInfoSendTo().split(",");
            if (receivers != null){
                // 为每个邮件接收者创建一个地址
                tos = new InternetAddress[receivers.length + 1];
                tos[0] = new InternetAddress(mailInfo.getEmailInfoSendTo());
                for (int i=0; i<receivers.length; i++){
                    tos[i+1] = new InternetAddress(receivers[i]);
                }
            } else {
                tos = new InternetAddress[1];
                tos[0] = new InternetAddress(mailInfo.getEmailInfoSendTo());
            }
            // 将所有接收者地址都添加到邮件接收者属性中
            message.setRecipients(Message.RecipientType.TO, tos);
           
   // 获取抄送者信息
            String[] ccs = mailInfo.getEmailInfoSendCc()!=null?mailInfo.getEmailInfoSendCc().split(","):null;
            if (ccs != null){
                // 为每个邮件接收者创建一个地址
                Address[] ccAdresses = new InternetAddress[ccs.length];
                for (int i=0; i<ccs.length; i++){
                    ccAdresses[i] = new InternetAddress(ccs[i]);
                }
                // 将抄送者信息设置到邮件信息中,注意类型为Message.RecipientType.CC
                message.setRecipients(Message.RecipientType.CC, ccAdresses);
            }
           
            // 获取密送者信息
            String[] bccs = mailInfo.getEmailInfoSendBcc()!=null?mailInfo.getEmailInfoSendBcc().split(","):null;
            if (bccs != null){
                // 为每个邮件接收者创建一个地址
                Address[] bccAdresses = new InternetAddress[bccs.length];
                for (int i=0; i<bccs.length; i++){
                    bccAdresses[i] = new InternetAddress(bccs[i]);
                }
                // 将抄送者信息设置到邮件信息中,注意类型为Message.RecipientType.CC
                message.setRecipients(Message.RecipientType.BCC, bccAdresses);
            }
  
   
            //主题
            message.setSubject(mailInfo.getEmailInfoSubject());
           
         // 设置发送时间
         message.setSentDate(new Date());
           
         // 设置邮件内容
         Multipart msgMultipart = new MimeMultipart();
   BodyPart htmlBodyPart = new MimeBodyPart();
   htmlBodyPart.setContent(mailInfo.getEmailInfoSendContent(), "text/html;charset=GBK");//内容
   msgMultipart.addBodyPart(htmlBodyPart);
   message.setContent(msgMultipart);
         
   //监控邮件发送过程
   session.setDebug(true);
   
   // 发送邮件
            Transport.send(message);
           

  }catch(Exception e){
   e.printStackTrace();
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值