天了嚕 又被這郵件擼了 來第二彈
需要個第三方的 jar包
JAF(版本 1.1.1) activation.jar -- 需要加附件才用得上 這個jar包
依賴地址:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.6</version>
</dependency>
---------------------------- 代碼如下
/**
* 郵箱工具類
*/
public class EmailUtils {
public static void main(String[] args ){
sendQQEmail("163賬號@163.com","測試","aaaaa111 aSsadasdad大三的");
sendQQEmail("企鵝賬號@qq.com","hello騰訊","aaa啊哈哈哈1212212大三的tuol級");
}
/**
* 用QQ 發送郵件
* @param to 收信人
* @param subject 標題
* @param context 文本
*/
public static void sendQQEmail(String to,String subject,String context) {
//跟smtp服務器建立一個連接
Properties p = new Properties();
// 設置郵件服務器主機名
p.setProperty("mail.host", "smtp.qq.com");//指定郵件服務器
// 發送服務器需要身份驗證
p.setProperty("mail.smtp.auth", "true");//要採用指定用戶名密碼的方式去認證
// 發送郵件協議名稱
p.setProperty("mail.transport.protocol", "smtp");
// 設置端口 默認端口 25
p.setProperty("mail.smtp.port", "465");
// 開啓SSL加密,否則會失敗
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
// 創建session
Session session = Session.getDefaultInstance(p, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//用戶名可以用QQ賬號也可以用郵箱的別名
PasswordAuthentication pa = new PasswordAuthentication("賬號@qq.com", "授權碼");
// 後面的字符是授權碼,用qq密碼不行!!
return pa;
}
});
try {
//聲明一個Message對象(代表一封郵件),從session中創建
MimeMessage msg = new MimeMessage(session);
//郵件信息封裝
//1發件人
msg.setFrom(new InternetAddress("賬號@qq.com"));
//2收件人
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
//3郵件內容:主題、內容
msg.setSubject(subject);
msg.setContent(context, "text/plain;charset=utf-8");//純文本
//發送動作
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 用163 發郵件
* @param to
* @param subject
* @param context
*/
public static void send163Email(String to,String subject,String context) {
//跟smtp服務器建立一個連接
Properties p = new Properties();
// 設置郵件服務器主機名
p.setProperty("mail.host", "smtp.163.com");//指定郵件服務器
// 發送服務器需要身份驗證
p.setProperty("mail.smtp.auth", "true");//要採用指定用戶名密碼的方式去認證
// 發送郵件協議名稱
p.setProperty("mail.transport.protocol", "smtp");
// 設置端口 默認端口 25
p.setProperty("mail.smtp.port", "994");// 也支持465 端口
// 開啓SSL加密,否則會失敗
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
// 創建session
Session session = Session.getDefaultInstance(p, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//用戶名,密碼
PasswordAuthentication pa = new PasswordAuthentication("賬號@163.com", "密碼");
return pa;
}
});
try {
//聲明一個Message對象(代表一封郵件),從session中創建
MimeMessage msg = new MimeMessage(session);
//郵件信息封裝
//1發件人
msg.setFrom(new InternetAddress("賬號@163.com"));
//2收件人
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
//3郵件內容:主題、內容
msg.setSubject(subject);
msg.setContent(context, "text/plain;charset=utf-8");//純文本
// msg.setContent(
// "Hello <a href='http://www.baidu.com?id=ddd'>你好,快樂嗎?<a/>",
// "text/html;charset=utf-8");//發html格式的文本
//發送動作
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
--------------------------- 有附件 的 郵件發送 -----------------------------------
public void send2() throws Exception{
//跟smtp服務器建立一個連接
Properties p = new Properties();
// 開啓debug調試,以便在控制檯查看
p.setProperty("mail.debug", "true");
p.setProperty("mail.host", "smtp.sina.com");//指定郵件服務器,默認端口 25
p.setProperty("mail.smtp.auth", "true");//要採用指定用戶名密碼的方式去認證
// 發送郵件協議名稱
p.setProperty("mail.transport.protocol", "smtp");
// 開啓SSL加密,否則會失敗
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
// 創建session
Session session = Session.getInstance(p);
// 通過session得到transport對象
Transport ts = session.getTransport();
// 連接郵件服務器:郵箱類型,帳號,授權碼代替密碼(更安全)
ts.connect("smtp.qq.com", "61*****29", "jnjt*******bdab");
// 後面的字符是授權碼,不能用qq密碼
//聲明一個Message對象(代表一封郵件),從session中創建
MimeMessage msg = new MimeMessage(session);
//郵件信息封裝
//1發件人
msg.setFrom( new InternetAddress("61******[email protected]") );
//2收件人
msg.setRecipient(RecipientType.TO, new InternetAddress("ch*****[email protected]") );
//3郵件內容:主題、內容
msg.setSubject("這是我用Java發來的郵件--帶附件的....");
//添加附件部分
//郵件內容部分1---文本內容
MimeBodyPart body0 = new MimeBodyPart(); //郵件中的文字部分
body0.setContent("這是兩張<font color='red'>圖片</font>....","text/html;charset=utf-8");
//郵件內容部分2---附件1
MimeBodyPart body1 = new MimeBodyPart(); //附件1
body1.setDataHandler( new DataHandler( new FileDataSource("./imgs/1.jpg")) );//./代表項目根目錄下
body1.setFileName( MimeUtility.encodeText("中文1.jpg") );//中文附件名,解決亂碼
//郵件內容部分3---附件2
MimeBodyPart body2 = new MimeBodyPart(); //附件2
body2.setDataHandler( new DataHandler( new FileDataSource("./imgs/2.jpg")) );
body2.setFileName("2.jpg");
//把上面的3部分組裝在一起,設置到msg中
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(body0);
mm.addBodyPart(body1);
mm.addBodyPart(body2);
msg.setContent(mm);
// 發送郵件
ts.sendMessage(msg,msg.getAllRecipients());
ts.close();
}
============================SpringBoot 發郵件================================
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml
spring:
mail:
host: smtp.qq.com
username: [email protected]
password: xxxx
protocol: smtp
郵件代碼
@GetMapping("/sendMail")
public String sendMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("[email protected]");
String[] toArray = {"[email protected]"};
message.setTo(toArray);
message.setSubject("這是標題");
message.setText("這是內容");
String[] ccArray = {"[email protected]"};
message.setCc(ccArray);
mailSender.send(message);
return "發送成功";
}
@GetMapping("/sendAttachMail")
public String sendAttachMail(){
String [] fileArray={"C:\\Users\\Qiang.Yu11\\Desktop\\新增表.xlsx", "C:\\Users\\Qiang.Yu11\\Desktop\\減少表.xlsx"};
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper mmh = new MimeMessageHelper(message,true);
String[] toArray = {"[email protected]"};
String[] ccArray = {"[email protected]"};
mmh.setFrom("[email protected]");
mmh.setTo(toArray);
mmh.setSubject("發送標題");
mmh.setText("發送內容");
mmh.setCc(ccArray);
FileSystemResource file = null;
for (int i = 0; i < fileArray.length; i++) {
//添加附件
file = new FileSystemResource(fileArray[i]);
mmh.addAttachment(file.getFilename(), file);
}
mailSender.send(message);
return "帶附件的郵件發送成功";
}catch (Exception e){
e.printStackTrace();
return "發送帶附件的郵件失敗";
}
}