吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2400|回复: 0
收起左侧

[Java 转载] 运维必备MAVEN项目,java连接数据库查询并定时发送邮件

  [复制链接]
墨迹未存 发表于 2019-3-13 14:33
本帖最后由 wushaominkk 于 2019-3-14 09:39 编辑

链接:https://pan.baidu.com/s/1f-m30hpe5UwJQCtUQRjQyA
提取码:pcy0
部分代码:
[Java] 纯文本查看 复制代码
/**
  * 发送邮件
  * @Param user 发件人邮箱
  * @param password 授权码(注意不是邮箱登录密码)
  * @param host 
  * @param from 发件人
  * @param to 接收者邮箱
  * @param subject 邮件主题
  * @param content 邮件内容
  * @Return success 发送成功 failure 发送失败
  * @throws Exception
  */
public String sendMail(String user, String password, String host,
   String from, String to, String subject, String content)
     throws Exception {
  if (to != null){
   Properties props = System.getProperties();
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.auth", "true");
   MailAuthenticator auth = new MailAuthenticator();
   MailAuthenticator.USERNAME = user;
   MailAuthenticator.PASSWORD = password;
   Session session = Session.getInstance(props, auth);
   session.setDebug(true);
   try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    if (!to.trim().equals(""))
     message.addRecipient(Message.RecipientType.TO,
       new InternetAddress(to.trim()));
    message.setSubject(subject);
    MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
    mbp1.setContent(content, "text/html;charset=utf-8");
    Multipart mp = new MimeMultipart(); // 整个邮件:正文+附件
    mp.addBodyPart(mbp1);
    // mp.addBodyPart(mbp2);
    message.setContent(mp);
    message.setSentDate(new Date());
    message.saveChanges();
    Transport trans = session.getTransport("smtp");
    trans.send(message);
    System.out.println(message.toString());

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-8 00:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表