欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)帶附件的郵件發(fā)送功能

 更新時(shí)間:2020年04月28日 10:24:43   作者:Mr丶Husky  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)帶附件的郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)郵件發(fā)送功能的具體代碼,供大家參考,具體內(nèi)容如下

1、需要導(dǎo)入mail.jar、activation.jar這兩個(gè)郵件發(fā)送的jar包,可在網(wǎng)上搜索并下載
2、需要設(shè)置相關(guān)郵箱服務(wù)器,我用的是QQ郵箱,操作如下所示:開啟相關(guān)服務(wù),并生產(chǎn)授權(quán)碼(這個(gè)代碼中會(huì)用到)。

代碼如下所示:

package com.ecg.controller; 
 
import java.io.UnsupportedEncodingException; 
import java.security.GeneralSecurityException; 
import java.util.Properties; 
 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Authenticator; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
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; 
 
import com.sun.mail.util.MailSSLSocketFactory; 
 
/** 
 * 郵件發(fā)送 
 * 
 * @author wanglongfei 
 * E-mail: islongfei@gmail.com 
 * @version 2017年8月27日 
 * 
 */ 
public class mailtest { 
 
 
 public static void main(String [] args) throws GeneralSecurityException, UnsupportedEncodingException 
 { 
 // 收件人電子郵箱 
 String to = "2528621082@qq.com"; 
 
 // 發(fā)件人電子郵箱 
 String from = "1135237317@qq.com"; 
 
 // 指定發(fā)送郵件的主機(jī)為 smtp.qq.com 
 String host = "smtp.qq.com"; //QQ 郵件服務(wù)器 
 
 // 獲取系統(tǒng)屬性 
 Properties properties = System.getProperties(); 
 
 // 設(shè)置郵件服務(wù)器 
 properties.setProperty("mail.smtp.host", host); 
 
 properties.put("mail.smtp.auth", "true"); 
 MailSSLSocketFactory sf = new MailSSLSocketFactory(); 
 sf.setTrustAllHosts(true); 
 properties.put("mail.smtp.ssl.enable", "true"); 
 properties.put("mail.smtp.ssl.socketFactory", sf); 
 // 獲取默認(rèn)session對(duì)象 
 Session session = Session.getDefaultInstance(properties,new Authenticator(){ 
 public PasswordAuthentication getPasswordAuthentication() 
 { //qq郵箱服務(wù)器賬戶、第三方登錄授權(quán)碼 
 return new PasswordAuthentication("1135237317@qq.com", "dgahhkkjrtgafejj"); //發(fā)件人郵件用戶名、密碼 
 } 
 }); 
 
 try{ 
 // 創(chuàng)建默認(rèn)的 MimeMessage 對(duì)象 
 MimeMessage message = new MimeMessage(session); 
 
 // Set From: 頭部頭字段 
 message.setFrom(new InternetAddress(from)); 
 
 // Set To: 頭部頭字段 
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
 
 // Set Subject: 主題文字 
 message.setSubject("家醫(yī)康心電診斷結(jié)果"); 
 
 // 創(chuàng)建消息部分 
 BodyPart messageBodyPart = new MimeBodyPart(); 
 
 // 消息 
 messageBodyPart.setText("233333333333333"); 
 
 // 創(chuàng)建多重消息 
 Multipart multipart = new MimeMultipart(); 
 
 // 設(shè)置文本消息部分 
 multipart.addBodyPart(messageBodyPart); 
 
 // 附件部分 
 messageBodyPart = new MimeBodyPart(); 
 //設(shè)置要發(fā)送附件的文件路徑 
 String filename = "C:/Users/下雨天-lalala/Desktop/家醫(yī)康心電圖/十二導(dǎo)聯(lián)同步心電圖-.png"; 
 DataSource source = new FileDataSource(filename); 
 messageBodyPart.setDataHandler(new DataHandler(source)); 
 
 //messageBodyPart.setFileName(filename); 
 //處理附件名稱中文(附帶文件路徑)亂碼問題 
 messageBodyPart.setFileName(MimeUtility.encodeText(filename)); 
 multipart.addBodyPart(messageBodyPart); 
 
 // 發(fā)送完整消息 
 message.setContent(multipart ); 
 
 // 發(fā)送消息 
 Transport.send(message); 
 System.out.println("Sent message successfully...."); 
 }catch (MessagingException mex) { 
 mex.printStackTrace(); 
 } 
 } 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論