Spring Boot整合郵件發(fā)送與注意事項(xiàng)
什么是Spring Boot
Spring Boot是一個(gè)框架,其設(shè)計(jì)目的是簡(jiǎn)化Spring應(yīng)用的初始搭建配置以及開發(fā)過(guò)程。該框架使用了特定的配置方式,從而使開發(fā)人員不在需要定義樣板化的配置。
Spring Boot的好處
1、配置簡(jiǎn)單;
2、編碼簡(jiǎn)單;
3、部署簡(jiǎn)單;
4、監(jiān)控簡(jiǎn)單;
概述
Spring Boot下面整合了郵件服務(wù)器,使用Spring Boot能夠輕松實(shí)現(xiàn)郵件發(fā)送;整理下最近使用Spring Boot發(fā)送郵件和注意事項(xiàng);
Maven包依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
Spring Boot的配置
spring.mail.host=smtp.servie.com spring.mail.username=用戶名 //發(fā)送方的郵箱 spring.mail.password=密碼 //對(duì)于qq郵箱而言 密碼指的就是發(fā)送方的授權(quán)碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=false #是否用啟用加密傳送的協(xié)議驗(yàn)證項(xiàng) spring.mail.properties.mail.smtp.starttls.required=fasle #是否用啟用加密傳送的協(xié)議驗(yàn)證項(xiàng) #注意:在spring.mail.password處的值是需要在郵箱設(shè)置里面生成的授權(quán)碼,這個(gè)不是真實(shí)的密碼。
Spring 代碼實(shí)現(xiàn)
package com.dbgo.webservicedemo.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Component("emailtool")
public class EmailTool {
@Autowired
private JavaMailSender javaMailSender;
public void sendSimpleMail(){
MimeMessage message = null;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("jiajinhao@dbgo.cn");
helper.setTo("653484166@qq.com");
helper.setSubject("標(biāo)題:發(fā)送Html內(nèi)容");
StringBuffer sb = new StringBuffer();
sb.append("<h1>大標(biāo)題-h1</h1>")
.append("<p style='color:#F00'>紅色字</p>")
.append("<p style='text-align:right'>右對(duì)齊</p>");
helper.setText(sb.toString(), true);
FileSystemResource fileSystemResource=new FileSystemResource(new File("D:\76678.pdf"))
helper.addAttachment("電子發(fā)票",fileSystemResource);
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
非Spring Boot下發(fā)送電子郵件:
Maven包依賴
<dependencies> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.2</version> </dependency> </dependencies>
DEMO1代碼事例
package com.justin.framework.core.utils.email;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 使用SMTP協(xié)議發(fā)送電子郵件
*/
public class sendEmailCode {
// 郵件發(fā)送協(xié)議
private final static String PROTOCOL = "smtp";
// SMTP郵件服務(wù)器
private final static String HOST = "mail.tdb.com";
// SMTP郵件服務(wù)器默認(rèn)端口
private final static String PORT = "25";
// 是否要求身份認(rèn)證
private final static String IS_AUTH = "true";
// 是否啟用調(diào)試模式(啟用調(diào)試模式可打印客戶端與服務(wù)器交互過(guò)程時(shí)一問(wèn)一答的響應(yīng)消息)
private final static String IS_ENABLED_DEBUG_MOD = "true";
// 發(fā)件人
private static String from = "tdbjrcrm@tdb.com";
// 收件人
private static String to = "db_yangruirui@tdbcwgs.com";
private static String senduserName="tdbjrcrm@tdb.com";
private static String senduserPwd="New*2016";
// 初始化連接郵件服務(wù)器的會(huì)話信息
private static Properties props = null;
static {
props = new Properties();
props.setProperty("mail.enable", "true");
props.setProperty("mail.transport.protocol", PROTOCOL);
props.setProperty("mail.smtp.host", HOST);
props.setProperty("mail.smtp.port", PORT);
props.setProperty("mail.smtp.auth", IS_AUTH);//視情況而定
props.setProperty("mail.debug",IS_ENABLED_DEBUG_MOD);
}
/**
* 發(fā)送簡(jiǎn)單的文本郵件
*/
public static boolean sendTextEmail(String to,int code) throws Exception {
try {
// 創(chuàng)建Session實(shí)例對(duì)象
Session session1 = Session.getDefaultInstance(props);
// 創(chuàng)建MimeMessage實(shí)例對(duì)象
MimeMessage message = new MimeMessage(session1);
// 設(shè)置發(fā)件人
message.setFrom(new InternetAddress(from));
// 設(shè)置郵件主題
message.setSubject("內(nèi)燃機(jī)注冊(cè)驗(yàn)證碼");
// 設(shè)置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 設(shè)置發(fā)送時(shí)間
message.setSentDate(new Date());
// 設(shè)置純文本內(nèi)容為郵件正文
message.setText("您的驗(yàn)證碼是:"+code+"!驗(yàn)證碼有效期是10分鐘,過(guò)期后請(qǐng)重新獲取!"
+ "中國(guó)內(nèi)燃機(jī)學(xué)會(huì)");
// 保存并生成最終的郵件內(nèi)容
message.saveChanges();
// 獲得Transport實(shí)例對(duì)象
Transport transport = session1.getTransport();
// 打開連接
transport.connect("meijiajiang2016", "");
// 將message對(duì)象傳遞給transport對(duì)象,將郵件發(fā)送出去
transport.sendMessage(message, message.getAllRecipients());
// 關(guān)閉連接
transport.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) throws Exception {
sendHtmlEmail("db_yangruirui@tdbcwgs.com", 88888);
}
/**
* 發(fā)送簡(jiǎn)單的html郵件
*/
public static boolean sendHtmlEmail(String to,int code) throws Exception {
// 創(chuàng)建Session實(shí)例對(duì)象
Session session1 = Session.getInstance(props, new MyAuthenticator());
// 創(chuàng)建MimeMessage實(shí)例對(duì)象
MimeMessage message = new MimeMessage(session1);
// 設(shè)置郵件主題
message.setSubject("內(nèi)燃機(jī)注冊(cè)");
// 設(shè)置發(fā)送人
message.setFrom(new InternetAddress(from));
// 設(shè)置發(fā)送時(shí)間
message.setSentDate(new Date());
// 設(shè)置收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
// 設(shè)置html內(nèi)容為郵件正文,指定MIME類型為text/html類型,并指定字符編碼為gbk
message.setContent("<div style='width: 600px;margin: 0 auto'><h3 style='color:#003E64; text-align:center; '>內(nèi)燃機(jī)注冊(cè)驗(yàn)證碼</h3><p style=''>尊敬的用戶您好:</p><p style='text-indent: 2em'>您在注冊(cè)內(nèi)燃機(jī)賬號(hào),此次的驗(yàn)證碼是:"+code+",有效期10分鐘!如果過(guò)期請(qǐng)重新獲取。</p><p style='text-align: right; color:#003E64; font-size: 20px;'>中國(guó)內(nèi)燃機(jī)學(xué)會(huì)</p></div>","text/html;charset=utf-8");
//設(shè)置自定義發(fā)件人昵稱
String nick="";
try {
nick=javax.mail.internet.MimeUtility.encodeText("中國(guó)內(nèi)燃機(jī)學(xué)會(huì)");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
message.setFrom(new InternetAddress(nick+" <"+from+">"));
// 保存并生成最終的郵件內(nèi)容
message.saveChanges();
// 發(fā)送郵件
try {
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 發(fā)送帶內(nèi)嵌圖片的HTML郵件
*/
public static void sendHtmlWithInnerImageEmail() throws MessagingException {
// 創(chuàng)建Session實(shí)例對(duì)象
Session session = Session.getDefaultInstance(props, new MyAuthenticator());
// 創(chuàng)建郵件內(nèi)容
MimeMessage message = new MimeMessage(session);
// 郵件主題,并指定編碼格式
message.setSubject("帶內(nèi)嵌圖片的HTML郵件", "utf-8");
// 發(fā)件人
message.setFrom(new InternetAddress(from));
// 收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
// 抄送
message.setRecipient(RecipientType.CC, new InternetAddress("java_test@sohu.com"));
// 密送 (不會(huì)在郵件收件人名單中顯示出來(lái))
message.setRecipient(RecipientType.BCC, new InternetAddress("417067629@qq.com"));
// 發(fā)送時(shí)間
message.setSentDate(new Date());
// 創(chuàng)建一個(gè)MIME子類型為“related”的MimeMultipart對(duì)象
MimeMultipart mp = new MimeMultipart("related");
// 創(chuàng)建一個(gè)表示正文的MimeBodyPart對(duì)象,并將它加入到前面創(chuàng)建的MimeMultipart對(duì)象中
MimeBodyPart htmlPart = new MimeBodyPart();
mp.addBodyPart(htmlPart);
// 創(chuàng)建一個(gè)表示圖片資源的MimeBodyPart對(duì)象,將將它加入到前面創(chuàng)建的MimeMultipart對(duì)象中
MimeBodyPart imagePart = new MimeBodyPart();
mp.addBodyPart(imagePart);
// 將MimeMultipart對(duì)象設(shè)置為整個(gè)郵件的內(nèi)容
message.setContent(mp);
// 設(shè)置內(nèi)嵌圖片郵件體
DataSource ds = new FileDataSource(new File("resource/firefoxlogo.png"));
DataHandler dh = new DataHandler(ds);
imagePart.setDataHandler(dh);
imagePart.setContentID("firefoxlogo.png"); // 設(shè)置內(nèi)容編號(hào),用于其它郵件體引用
// 創(chuàng)建一個(gè)MIME子類型為"alternative"的MimeMultipart對(duì)象,并作為前面創(chuàng)建的htmlPart對(duì)象的郵件內(nèi)容
MimeMultipart htmlMultipart = new MimeMultipart("alternative");
// 創(chuàng)建一個(gè)表示html正文的MimeBodyPart對(duì)象
MimeBodyPart htmlBodypart = new MimeBodyPart();
// 其中cid=androidlogo.gif是引用郵件內(nèi)部的圖片,即imagePart.setContentID("androidlogo.gif");方法所保存的圖片
htmlBodypart.setContent("<span style='color:red;'>這是帶內(nèi)嵌圖片的HTML郵件哦!?。?lt;img src=\"cid:firefoxlogo.png\" /></span>","text/html;charset=utf-8");
htmlMultipart.addBodyPart(htmlBodypart);
htmlPart.setContent(htmlMultipart);
// 保存并生成最終的郵件內(nèi)容
message.saveChanges();
// 發(fā)送郵件
Transport.send(message);
}
/**
* 發(fā)送帶內(nèi)嵌圖片、附件、多收件人(顯示郵箱姓名)、郵件優(yōu)先級(jí)、閱讀回執(zhí)的完整的HTML郵件
*/
public static void sendMultipleEmail() throws Exception {
String charset = "utf-8"; // 指定中文編碼格式
// 創(chuàng)建Session實(shí)例對(duì)象
Session session = Session.getInstance(props,new MyAuthenticator());
// 創(chuàng)建MimeMessage實(shí)例對(duì)象
MimeMessage message = new MimeMessage(session);
// 設(shè)置主題
message.setSubject("使用JavaMail發(fā)送混合組合類型的郵件測(cè)試");
// 設(shè)置發(fā)送人
message.setFrom(new InternetAddress(from,"新浪測(cè)試郵箱",charset));
// 設(shè)置收件人
message.setRecipients(RecipientType.TO,
new Address[] {
// 參數(shù)1:郵箱地址,參數(shù)2:姓名(在客戶端收件只顯示姓名,而不顯示郵件地址),參數(shù)3:姓名中文字符串編碼
new InternetAddress("java_test@sohu.com", "張三_sohu", charset),
new InternetAddress("xyang0917@163.com", "李四_163", charset),
}
);
// 設(shè)置抄送
message.setRecipient(RecipientType.CC, new InternetAddress("xyang0917@gmail.com","王五_gmail",charset));
// 設(shè)置密送
message.setRecipient(RecipientType.BCC, new InternetAddress("xyang0917@qq.com", "趙六_QQ", charset));
// 設(shè)置發(fā)送時(shí)間
message.setSentDate(new Date());
// 設(shè)置回復(fù)人(收件人回復(fù)此郵件時(shí),默認(rèn)收件人)
message.setReplyTo(InternetAddress.parse("\"" + MimeUtility.encodeText("田七") + "\" <417067629@qq.com>"));
// 設(shè)置優(yōu)先級(jí)(1:緊急 3:普通 5:低)
message.setHeader("X-Priority", "1");
// 要求閱讀回執(zhí)(收件人閱讀郵件時(shí)會(huì)提示回復(fù)發(fā)件人,表明郵件已收到,并已閱讀)
message.setHeader("Disposition-Notification-To", from);
// 創(chuàng)建一個(gè)MIME子類型為"mixed"的MimeMultipart對(duì)象,表示這是一封混合組合類型的郵件
MimeMultipart mailContent = new MimeMultipart("mixed");
message.setContent(mailContent);
// 附件
MimeBodyPart attach1 = new MimeBodyPart();
MimeBodyPart attach2 = new MimeBodyPart();
// 內(nèi)容
MimeBodyPart mailBody = new MimeBodyPart();
// 將附件和內(nèi)容添加到郵件當(dāng)中
mailContent.addBodyPart(attach1);
mailContent.addBodyPart(attach2);
mailContent.addBodyPart(mailBody);
// 附件1(利用jaf框架讀取數(shù)據(jù)源生成郵件體)
DataSource ds1 = new FileDataSource("resource/Earth.bmp");
DataHandler dh1 = new DataHandler(ds1);
attach1.setFileName(MimeUtility.encodeText("Earth.bmp"));
attach1.setDataHandler(dh1);
// 附件2
DataSource ds2 = new FileDataSource("resource/如何學(xué)好C語(yǔ)言.txt");
DataHandler dh2 = new DataHandler(ds2);
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText("如何學(xué)好C語(yǔ)言.txt"));
// 郵件正文(內(nèi)嵌圖片+html文本)
MimeMultipart body = new MimeMultipart("related"); //郵件正文也是一個(gè)組合體,需要指明組合關(guān)系
mailBody.setContent(body);
// 郵件正文由html和圖片構(gòu)成
MimeBodyPart imgPart = new MimeBodyPart();
MimeBodyPart htmlPart = new MimeBodyPart();
body.addBodyPart(imgPart);
body.addBodyPart(htmlPart);
// 正文圖片
DataSource ds3 = new FileDataSource("resource/firefoxlogo.png");
DataHandler dh3 = new DataHandler(ds3);
imgPart.setDataHandler(dh3);
imgPart.setContentID("firefoxlogo.png");
// html郵件內(nèi)容
MimeMultipart htmlMultipart = new MimeMultipart("alternative");
htmlPart.setContent(htmlMultipart);
MimeBodyPart htmlContent = new MimeBodyPart();
htmlContent.setContent(
"<span style='color:red'>這是我自己用java mail發(fā)送的郵件哦!" +
"<img src='cid:firefoxlogo.png' /></span>"
, "text/html;charset=gbk");
htmlMultipart.addBodyPart(htmlContent);
// 保存郵件內(nèi)容修改
message.saveChanges();
/*File eml = buildEmlFile(message);
sendMailForEml(eml);*/
// 發(fā)送郵件
Transport.send(message);
}
/**
* 將郵件內(nèi)容生成eml文件
* @param message 郵件內(nèi)容
*/
public static File buildEmlFile(Message message) throws MessagingException, FileNotFoundException, IOException {
File file = new File("c:\\" + MimeUtility.decodeText(message.getSubject())+".eml");
message.writeTo(new FileOutputStream(file));
return file;
}
/**
* 發(fā)送本地已經(jīng)生成好的email文件
*/
public static void sendMailForEml(File eml) throws Exception {
// 獲得郵件會(huì)話
Session session = Session.getInstance(props,new MyAuthenticator());
// 獲得郵件內(nèi)容,即發(fā)生前生成的eml文件
InputStream is = new FileInputStream(eml);
MimeMessage message = new MimeMessage(session,is);
//發(fā)送郵件
Transport.send(message);
}
/**
* 向郵件服務(wù)器提交認(rèn)證信息
*/
static class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator() {
super();
this.password=senduserPwd;
this.username=senduserName;
}
public MyAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
}
DEMO2代碼事例:
package com.justin.framework.core.utils.email;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MailManagerUtils {
//發(fā)送郵件
public static boolean sendMail(Email email) {
String subject = email.getSubject();
String content = email.getContent();
String[] recievers = email.getRecievers();
String[] copyto = email.getCopyto();
String attbody = email.getAttbody();
String[] attbodys = email.getAttbodys();
if(recievers == null || recievers.length <=0) {
return false;
}
try {
Properties props =new Properties();
props.setProperty("mail.enable", "true");
props.setProperty("mail.protocal", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.user", "tdbjrcrm@tdb.com");
props.setProperty("mail.pass", "New***");
props.setProperty("mail.smtp.host","mail.tdb.com");
props.setProperty("mail.smtp.from","tdbjrcrm@tdb.com");
props.setProperty("mail.smtp.fromname","tdbVC");
// 創(chuàng)建一個(gè)程序與郵件服務(wù)器的通信
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
// 設(shè)置發(fā)送人和接受人
Address sender = new InternetAddress(props.getProperty("mail.smtp.from"));
// 多個(gè)接收人
msg.setFrom(sender);
Set<InternetAddress> toUserSet = new HashSet<InternetAddress>();
// 郵箱有效性較驗(yàn)
for (int i = 0; i < recievers.length; i++) {
if (recievers[i].trim().matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)+$")) {
toUserSet.add(new InternetAddress(recievers[i].trim()));
}
}
msg.setRecipients(Message.RecipientType.TO, toUserSet.toArray(new InternetAddress[0]));
// 設(shè)置抄送
if (copyto != null) {
Set<InternetAddress> copyToUserSet = new HashSet<InternetAddress>();
// 郵箱有效性較驗(yàn)
for (int i = 0; i < copyto.length; i++) {
if (copyto[i].trim().matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)+$")) {
copyToUserSet.add(new InternetAddress(copyto[i].trim()));
}
}
// msg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
msg.setRecipients(Message.RecipientType.CC, copyToUserSet.toArray(new InternetAddress[0]));
}
// 設(shè)置郵件主題
msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B")); // 中文亂碼問(wèn)題
// 設(shè)置郵件內(nèi)容
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html; charset=UTF-8"); // 中文
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
/********************** 發(fā)送附件 ************************/
if (attbody != null) {
String[] filePath = attbody.split(";");
for (String filepath : filePath) {
//設(shè)置信件的附件(用本地機(jī)上的文件作為附件)
BodyPart mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filepath);
DataHandler dh = new DataHandler(fds);
mdp.setFileName(MimeUtility.encodeText(fds.getName()));
mdp.setDataHandler(dh);
multipart.addBodyPart(mdp);
}
//把mtp作為消息對(duì)象的內(nèi)容
msg.setContent(multipart);
};
if (attbodys != null) {
for (String filepath : attbodys) {
//設(shè)置信件的附件(用本地機(jī)上的文件作為附件)
BodyPart mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filepath);
DataHandler dh = new DataHandler(fds);
mdp.setFileName(MimeUtility.encodeText(fds.getName()));
mdp.setDataHandler(dh);
multipart.addBodyPart(mdp);
}
//把mtp作為消息對(duì)象的內(nèi)容
msg.setContent(multipart);
}
;
/********************** 發(fā)送附件結(jié)束 ************************/
// 先進(jìn)行存儲(chǔ)郵件
msg.saveChanges();
System.out.println("正在發(fā)送郵件....");
Transport trans = mailConnection.getTransport(props.getProperty("mail.protocal"));
// 郵件服務(wù)器名,用戶名,密碼
trans.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"), props.getProperty("mail.pass"));
trans.sendMessage(msg, msg.getAllRecipients());
System.out.println("發(fā)送郵件成功!");
// 關(guān)閉通道
if (trans.isConnected()) {
trans.close();
}
return true;
} catch (Exception e) {
System.err.println("郵件發(fā)送失??!" + e);
return false;
} finally {
}
}
// 發(fā)信人,收信人,回執(zhí)人郵件中有中文處理亂碼,res為獲取的地址
// http默認(rèn)的編碼方式為ISO8859_1
// 對(duì)含有中文的發(fā)送地址,使用MimeUtility.decodeTex方法
// 對(duì)其他則把地址從ISO8859_1編碼轉(zhuǎn)換成gbk編碼
public static String getChineseFrom(String res) {
String from = res;
try {
if (from.startsWith("=?GB") || from.startsWith("=?gb") || from.startsWith("=?UTF")) {
from = MimeUtility.decodeText(from);
} else {
from = new String(from.getBytes("ISO8859_1"), "GBK");
}
} catch (Exception e) {
e.printStackTrace();
}
return from;
}
// 轉(zhuǎn)換為GBK編碼
public static String toChinese(String strvalue) {
try {
if (strvalue == null)
return null;
else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
Email email=new Email();
email.setRecievers(new String[]{"db_yangruirui@tdbcwgs.com"});
email.setSubject("TEST測(cè)件");
email.setContent("TEST測(cè)試");
sendMail(email);
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot集成E-mail發(fā)送各種類型郵件
- SpringBoot實(shí)現(xiàn)發(fā)送郵件功能
- SpringBoot發(fā)送郵件功能 驗(yàn)證碼5分鐘過(guò)期
- 基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring Boot實(shí)戰(zhàn)之發(fā)送郵件示例代碼
- Spring Boot實(shí)現(xiàn)郵件發(fā)送功能
- Springboot實(shí)現(xiàn)郵件發(fā)送功能
- SpringBoot實(shí)現(xiàn)郵件發(fā)送功能的姿勢(shì)分享
相關(guān)文章
Java中ArrayBlockingQueue和LinkedBlockingQueue
這篇文章主要介紹了Java中ArrayBlockingQueue和LinkedBlockingQueue,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
java?for循環(huán)內(nèi)執(zhí)行多線程問(wèn)題
這篇文章主要介紹了java?for循環(huán)內(nèi)執(zhí)行多線程問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Security基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)認(rèn)證過(guò)程解析
這篇文章主要介紹了Spring Security基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)認(rèn)證過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程
Spring Boot支持容器的自動(dòng)配置,默認(rèn)是Tomcat,當(dāng)然我們也是可以進(jìn)行修改的。這篇文章給大家介紹了spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程,需要的朋友參考下吧2018-01-01
spring boot實(shí)現(xiàn)過(guò)濾器和攔截器demo
本篇文章主要介紹了spring boot實(shí)現(xiàn)過(guò)濾器和攔截器demo ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
SpringBoot2整合Ehcache組件實(shí)現(xiàn)輕量級(jí)緩存管理
EhCache是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、上手簡(jiǎn)單等特點(diǎn),是Hibernate中默認(rèn)的緩存提供方。本文講述下SpringBoot2 整合Ehcache組件的步驟2021-06-06
Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

