2020年11月23日星期一

JAVA实现邮箱推送工具(支持群发)

  1. 需要引用的jar包;
    <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>

  2. 工具类;
    `package cn.qin.xxltest.xxltask.service;
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import java.util.Properties;

public class Email {

private static final String MAIL_USER = "******@qq.com"; //邮件服务器登录用户名private static final String MAIL_PASSWORD = "bybtfpssyfvahrbeid"; //邮件服务器登录密码private static final String MAIL_FROM_SMTP = "939634038@qq.com"; //发送邮件地址public void sendmail(String[] mailArray,String subject,String content){ Properties props = new Properties(); //设置服务器验证 props.setProperty("mail.smtp.auth", "true"); //设置传输协议 props.setProperty("mail.transport.protocol", "smtp"); //选择服务类型 props.setProperty("mail.host", "smtp.qq.com"); //通过认证创建一个session实例 Session session = Session.getInstance(props,   new Authenticator()   {    protected PasswordAuthentication getPasswordAuthentication()    {     return new PasswordAuthentication(MAIL_USER,MAIL_PASSWORD);    }   } ); //显示邮件发送过程中的交互信息 session.setDebug(true); Message msg = new MimeMessage(session); Transport transport=null; try {  //邮件发送人  msg.setFrom(new InternetAddress(MAIL_FROM_SMTP));  //邮件主题  msg.setSubject(subject);  //邮件内容  msg.setText(content);  int len=mailArray.length;  InternetAddress address[]=new InternetAddress[len];  for (int i = 0; i < mailArray.length; i++) {   address[i]=new InternetAddress(mailArray[i]);  }  //邮件接收方  msg.addRecipients(Message.RecipientType.TO, address);  transport.send(msg); } catch (Exception e) {  e.printStackTrace(); }finally{  try {   if(transport!=null){    transport.close();   }  } catch (Exception e) {   // TODO Auto-generated catch block   e.printStackTrace();  } }}public static void main(String[] args) throws Exception{ Email demo2=new Email(); String[] mailArray ={"******@qq.com","******@eautosh.com"}; String subject="冬天来啦!"; String content="Hello world啊!"; demo2.sendmail(mailArray,subject,content);}

}`
3.qq邮件服务器登录密码获取方式;
第一步:登录邮箱,点击设置;

第二步:选择账户;

第三步:开启POP3/SMTP服务,获取密码









原文转载:http://www.shaoqun.com/a/492880.html

blackbird:https://www.ikjzd.com/w/950

贝贝母婴网:https://www.ikjzd.com/w/1321

启明星:https://www.ikjzd.com/w/1436


需要引用的jar包;<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency>工具类;`packagecn.qin.xxltest.xxltask.
贝贝网:贝贝网
上海跨境通:上海跨境通
中国吉林延边餐饮介绍 - :中国吉林延边餐饮介绍 -
自驾游途中常常憋尿 男子患急性尿路感染:自驾游途中常常憋尿 男子患急性尿路感染
柬埔寨文化及风俗 - :柬埔寨文化及风俗 -

没有评论:

发表评论