JavaMail发送邮件 多人发送

  1. 在咱们开发过程当中常常会用到邮件,好比 : 发送通知,找回密码 验证码  等等,再次总结了使用javaMail发送邮件,无需单间james等邮件服务器也可发送邮件

  2. javaMail官网 在里面能够找到详细的文档以及案例和jar包

  3. 咱们都知道在先后端交互都是有协议的,http协议,JavaMail也有本身的协议,SMTP/POP3和IMAP

  4. 使用javaMail前提是能够链接外网.

  5. 废话很少说,直接上案例:

    1.   导入依赖,在没使用maven的话导入相应的jar包,点此下载

      <dependency>
           <groupId>com.sun.mail</groupId>
           <artifactId>javax.mail</artifactId>
           <version>1.5.2</version>
      </dependency>

       

    2. 测试案例:这是一个模板工具

      package com.bgi.util;
      
      import org.springframework.core.io.ClassPathResource;
      
      import javax.mail.Address;
      import javax.mail.Session;
      import javax.mail.Transport;
      import javax.mail.internet.InternetAddress;
      import javax.mail.internet.MimeMessage;
      import java.io.IOException;
      import java.util.Date;
      import java.util.Properties;
      
      public class EmailUtil {
         //获取属性文件中的值,建议把配置的信息放到属性文件中,方便修改和获取
      private static Properties properties = new Properties(); static{ try {
         //加在属性文件 properties.load(
      new ClassPathResource("properties/email.properties").getInputStream()); } catch (IOException e) { } } public static String SMTPSERVER = properties.getProperty("smtp.server"); //从属性文件中获取值其中key为smtp.server public static String SMTPPORT = properties.getProperty("smtp.port"); //端口号 465 465 465 不是456 public static String ACCOUT = properties.getProperty("smtp.account");//帐户名:个人是163帐户,此帐户必须在设置中开启受权码受权 public static String PWD = properties.getProperty("smtp.pwd"); //受权密码 public static String users = properties.getProperty("email.users"); //这里是发送给多个用户多个用户用都好分割xxx@xx.com,xxx@xx.com public static void sendEmail(String content){ try { // 建立邮件配置 Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求) props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址 props.setProperty("mail.smtp.port", SMTPPORT); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.auth", "true"); // 须要请求认证 props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl // 根据邮件配置建立会话,注意session别导错包 Session session = Session.getDefaultInstance(props); // 开启debug模式,能够看到更多详细的输入日志 session.setDebug(true); //建立邮件 MimeMessage message = createEmail(session,users,content); //将用户和内容传递过来 //获取传输通道 Transport transport = session.getTransport(); transport.connect(SMTPSERVER,ACCOUT, PWD); //链接,并发送邮件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } } public static MimeMessage createEmail(Session session,String users,String content) throws Exception { // 根据会话建立邮件 MimeMessage msg = new MimeMessage(session); // address邮件地址, personal邮件昵称, charset编码方式 InternetAddress fromAddress = new InternetAddress(ACCOUT, "中间件推送", "utf-8"); // 设置发送邮件方 msg.setFrom(fromAddress); // 单个能够直接这样建立 // InternetAddress receiveAddress = new InternetAddress(); // 设置邮件接收方 Address[] internetAddressTo = new InternetAddress().parse(users);
         //type:
      要被设置为 TO, CC 或者 BCC,这里 CC 表明抄送、BCC 表明秘密抄送。举例:Message.RecipientType.TO
      
              msg.setRecipients(MimeMessage.RecipientType.TO,  internetAddressTo);
              // 设置邮件标题
              msg.setSubject("测试标题", "utf-8");
              msg.setText(content);
              // 设置显示的发件时间
              msg.setSentDate(new Date());
              // 保存设置
              msg.saveChanges();
              return msg;
          }
      
      }

       

    3. email.properties

      smtp.server=smtp.163.com
      smtp.port=465
      smtp.account=xxx@163.com
      smtp.pwd=xxxxx
      
      email.users=xxx@163.com,xxxx@qq.com,xxx@xx.cn

       

    4. 163邮箱开启受权:

         163邮箱示例 

    1. qq邮箱开启受权: 点击生成受权码,便可生成受权码

image 

 

到此已经能够发送邮件了,若是须要添加附件,能够自行研究,

相关文章
相关标签/搜索