java发送html模板
生活随笔
收集整理的這篇文章主要介紹了
java发送html模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先看一下模板:
?引入一下郵件依賴:
?<dependency><groupId>org.apache.commons</groupId><artifactId>commons-email</artifactId><version>1.5</version></dependency>?eamil-template.html模板文件
模板中的{0}、{1}這樣的占位符后面java代碼會替換掉
<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title></head><body style="color: #666; font-size: 14px; font-family: 'Open Sans',Helvetica,Arial,sans-serif;"><div class="box-content" style="width: 80%; margin: 20px auto; max-width: 800px; min-width: 600px;"><div class="header-tip" style="font-size: 12px;color: #aaa;text-align: right;padding-right: 25px;padding-bottom: 10px;">中國動物疫病預防控制中心</div><div class="info-top" style="padding: 15px 25px;border-top-left-radius: 10px;border-top-right-radius: 10px;background: rgb(0,128,128);color: #fff;overflow: hidden;line-height: 32px;"><img src="https://img-blog.csdnimg.cn/2022010700564968279.png" style="float: left; margin: 0 10px 0 0; width: 32px;" /><div style="color:white"><strong>SDK服務離線通知</strong></div></div><div class="info-wrap" style="border-bottom-left-radius: 10px;border-bottom-right-radius: 10px;border:1px solid #ddd;overflow: hidden;padding: 15px 15px 20px;"><div class="tips" style="padding:15px;"><p style=" list-style: '100%'; margin: 10px 0;">Hi 您好:</p><p style=" list-style: '100%'; margin: 10px 0;">您管理的服務:{0} ,已經離線!</p></div><div class="time" style="text-align: right; color: #999; padding: 0 15px 15px;">離線時間:{1}</div><br></div></div></body></html>位置如圖所示:
?
編寫郵件發送工具類apacheEmail.java:
import lombok.extern.slf4j.Slf4j; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component;import javax.mail.MessagingException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date;/*** @author qushen* @date 2021/6/7 13:43*/ @Slf4j @Configuration public class apacheEmail {@Value("${mail.server}")private String server;@Value("${mail.username}")private String username;@Value("${mail.password}")private String password;@Value("${mail.sender}")private String sender;public void sendMail(String receiver, String subject, String context) throws IOException, MessagingException, EmailException {HtmlEmail email=new HtmlEmail();//創建一個HtmlEmail實例對象email.setHostName(server);//郵箱的SMTP服務器,一般123郵箱的是smtp.123.com,qq郵箱為smtp.qq.comemail.setCharset("utf-8");//設置發送的字符類型email.addTo(receiver);//設置收件人email.setFrom(sender,"服務");//發送人的郵箱為自己的,用戶名可以隨便填email.setAuthentication(sender,password);//設置發送人到的郵箱和用戶名和授權碼(授權碼是自己設置的)email.setSubject(subject);//設置發送主題email.setHtmlMsg(context);email.send();log.info("郵件發送成功:{}", receiver);}}application.properties中的郵件配置:
mail.server=smtp.88.com mail.sender=qushen@88.com mail.username=qushen@88.com mail.password=tgyGHxxxxxxxx最后就是發送獲取html模板并發送的郵件的代碼
import com.sws.sdkemail.email.apacheEmail; import lombok.extern.log4j.Log4j2; import org.apache.commons.mail.EmailException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import javax.mail.MessagingException; import java.io.*; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.Date;/*** @author qushen* @date 2021/6/8 10:40*/ @Component @Log4j2 public class Test {@Autowiredprivate apacheEmail apcemail;@Scheduled(cron = "0 0/1 * * * ?")public void emailTask() throws EmailException, MessagingException, IOException {final String path = "static/eamil-template.html";ClassPathResource resource = new ClassPathResource(path);InputStream is = resource.getInputStream();Reader reader = new InputStreamReader(is);BufferedReader htmlReader = new BufferedReader(reader);StringBuffer buffer = new StringBuffer();String line = "";try {while ((line = htmlReader.readLine()) != null) {buffer.append(line);}} catch (Exception e) {log.error("讀取文件失敗,fileName:{}", path, e);} finally {is.close();htmlReader.close();}SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString = formatter.format(new Date());String htmlText = MessageFormat.format(buffer.toString(), "陜西", dateString);apcemail.sendMail("qushencn@qq.com", "服務離線通知", htmlText);}}?
?
總結
以上是生活随笔為你收集整理的java发送html模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一斤肉有多大
- 下一篇: Hadoop四大组件