}
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的克井網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!package com.nine.util;
import java.util.Random;
public class RandomUtil {
//生成隨機(jī)數(shù)字和字母
public static String getStringRandom() {
String val = "";
Random random = new Random();
//參數(shù)length,表示生成幾位隨機(jī)數(shù)
for(int i = 0; i < 4; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//輸出字母還是數(shù)字
if( "char".equalsIgnoreCase(charOrNum) ) {
//輸出是大寫(xiě)字母還是小寫(xiě)字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char)(random.nextInt(26) + temp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
}
package com.nine.pojo;
public class MyEmail {
private static MyEmail email;
private String host = "smtp.163.com"; // 發(fā)送方郵箱host
private String from = "17600246280@163.com"; // 發(fā)送方郵箱
private String user = "17600246280"; // 發(fā)送方郵箱賬號(hào)
private String pwd = "ovel1314.21"; // 發(fā)送方郵箱密碼
public static MyEmail getEmail(){
if(email!=null){
return email;
}else{
email = new MyEmail();
return email;
}
}
public String getFrom() {
return from;
}
public String getUser() {
return user;
}
public String getPwd() {
return pwd;
}
public String getHost() {
return host;
}
}
package com.nine.service;
public interface ISendEmailService {
/**
* 寫(xiě)一個(gè)發(fā)送郵件的方法
* @param content
* @param title
* @param address
* @param affix
* @param affixName
*/
void send(String content, String title, String address, String affix, String affixName);
}
package com.nine.service.impl;
import com.nine.pojo.MyEmail;
import com.nine.service.ISendEmailService;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class SendEmailServiceImpl implements ISendEmailService {
@Override
public void send(String content, String title, String address, String affix, String affixName) {
MyEmail myEmail = MyEmail.getEmail();
String host = myEmail.getHost();
String user = myEmail.getUser();
String pwd = myEmail.getPwd();
String from = myEmail.getFrom();
Properties props = new Properties();
// 設(shè)置發(fā)送郵件的郵件服務(wù)器的屬性(這里使用網(wǎng)易的smtp服務(wù)器) -->需要修改
props.put("mail.smtp.host", host);
// 需要經(jīng)過(guò)授權(quán),也就是有戶(hù)名和密碼的校驗(yàn),這樣才能通過(guò)驗(yàn)證(一定要有這一條)
props.put("mail.smtp.auth", "true");
// 用剛剛設(shè)置好的props對(duì)象構(gòu)建一個(gè)session
Session session = Session.getDefaultInstance(props);
// 有了這句便可以在發(fā)送郵件的過(guò)程中在console處顯示過(guò)程信息,供調(diào)試使
// 用(你可以在控制臺(tái)(console)上看到發(fā)送郵件的過(guò)程)
session.setDebug(true);
// 用session為參數(shù)定義消息對(duì)象
MimeMessage message = new MimeMessage(session);
try {
// 加載發(fā)件人地址 -->需要修改
message.setFrom(new InternetAddress(from));
// 加載收件人地址 -->需要修改
message.addRecipients(Message.RecipientType.TO, address);
List
String []median=address.split(",");//對(duì)輸入的多個(gè)郵件進(jìn)行逗號(hào)分割
for(int i=0;i
}
InternetAddress[] addresses =(InternetAddress[])list.toArray(new InternetAddress[list.size()]);
message.addRecipients(Message.RecipientType.TO, addresses);
// 加載標(biāo)題 --->也需要修改
message.setSubject(title);
// 向multipart對(duì)象中添加郵件的各個(gè)部分內(nèi)容,包括文本內(nèi)容和附件
Multipart multipart = new MimeMultipart();
// 設(shè)置郵件的文本內(nèi)容
BodyPart contentPart = new MimeBodyPart();
//需要修改的地方 寫(xiě)的內(nèi)容
contentPart.setText(content);
multipart.addBodyPart(contentPart);
// 添加附件
if(affix != null && !"".equals(affix))
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(affix);
// 添加附件的內(nèi)容
messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的標(biāo)題
// 這里很重要,通過(guò)下面的Base64編碼的轉(zhuǎn)換可以保證你的中文附件標(biāo)題名在發(fā)送時(shí)不會(huì)變成亂碼
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"+ enc.encode(affixName.getBytes()) + "?=");
multipart.addBodyPart(messageBodyPart);
}
// 將multipart對(duì)象放到message中
message.setContent(multipart);
// 保存郵件
message.saveChanges();
// 發(fā)送郵件
Transport transport = session.getTransport("smtp");
// 連接服務(wù)器的郵箱
transport.connect(host, user, pwd);
// 把郵件發(fā)送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
from=kojikazama@163.com
user=kojikazama
pwd=zhuhaojie9015
host=smpt.163.com
//發(fā)送二維碼驗(yàn)證
@RequestMapping(value = "sendCode",method = RequestMethod.GET)
@ResponseBody
public Map
String title="XXX資訊后臺(tái)管理系統(tǒng)";
String code= RandomUtil.getStringRandom();
String content="歡迎注冊(cè)本平臺(tái),你的驗(yàn)證碼是:"+code;
TwoDimensionCode.QRCodeTest qrCodeTest = new TwoDimensionCode.QRCodeTest();
qrCodeTest.testEncodeToEmail(content,300,300,"piao","png",title,email);
Map
map.put("checkCode",",code);
return map;
}