javax.mail发送邮件



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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javax.mailJava Mail API的一部分,是Java EE平台中发送电子邮件的标准API。 它提供了一个框架来处理邮件,并支持协议如SMTP,POP3和IMAP。以下是如何在Java中使用javax.mail的示例: 1.添加javax.mail依赖项到您的项目中。 2.编写以下代码来发送电子邮件: ```java import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendEmail { public static void main(String [] args) { // 收件人的电子邮件ID String to = "abcd@gmail.com"; // 发件人的电子邮件ID String from = "web@gmail.com"; // 假设您是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象 Session session = Session.getDefaultInstance(properties); try { // 创建默认的MimeMessage对象 MimeMessage message = new MimeMessage(session); // 设置From:header字段 message.setFrom(new InternetAddress(from)); // 设置To:header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置主题 message.setSubject("这是电子邮件主题"); // 设置实际消息 message.setText("这是实际消息"); // 发送消息 Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 以上代码将向收件人发送一封包含指定主题和正文的电子邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值