JavaMail锛欽ava鐨勯偖浠跺鐞咥PI璇﹁В
JavaMail鏄竴涓紑婧愮殑Java API锛岀敤浜庡鐞嗛偖浠剁殑鍙戦€佸拰鎺ユ敹锛岃鍖呭惈鍦↗avaMail/JSR 204椤圭洰涓€傚畠涓篔ava寮€鍙戣€呮彁渚涗簡涓€缁勪赴瀵岀殑API锛屾敮鎸丼MTP銆丳OP3銆両MAP绛夊绉嶉偖浠跺崗璁紝鑳借交鏉惧疄鐜伴偖浠剁殑鍙戦€併€佹帴鏀躲€佸鐞嗙瓑鎿嶄綔銆傛帴涓嬫潵锛岃鎴戜滑娣卞叆浜嗚ВJavaMail鐨勫伐浣滃師鐞嗭紝骞堕€氳繃瀹為檯绀轰緥鏋勫缓涓€涓偖浠跺彂閫佺郴缁熴€?/p>
搴旂敤棰嗗煙骞挎硾
JavaMail鐨勫簲鐢ㄩ鍩熺浉褰撳箍娉涳紝鍙互鍦ㄨ嚜鍔ㄩ€氱煡绯荤粺銆佹姤鍛婄郴缁熴€佺敤鎴峰弽棣堛€佽韩浠介獙璇佷互鍙婃秷鎭€氱煡绛夋柟闈㈠彂鎸ュ法澶т綔鐢ㄣ€備緥濡傦紝褰撴煇浜涚壒瀹氫簨浠跺彂鐢熸椂锛屽彲浠ラ€氳繃JavaMail鑷姩鍙戦€侀€氱煡閭欢缁欑浉鍏充汉鍛橈紱瀹氭湡鐨勭敤鎴锋姤鍛婃垨绯荤粺鏇存柊淇℃伅涔熷彲浠ラ€氳繃閭欢鍙戦€佺粰鐢ㄦ埛鎴栧洟闃熸垚鍛樸€傚畠杩樺彲浠ョ敤浜庢敹闆嗙敤鎴峰弽棣堝苟鍙戦€佺粰寮€鍙戝洟闃熸垨瀹㈡湇閮ㄩ棬锛屾垨鍦ㄧ敤鎴锋敞鍐屻€佸瘑鐮侀噸缃瓑娴佺▼涓彂閫佸寘鍚獙璇佺爜鐨勯偖浠躲€?/p>
鏍稿績缁勪欢浠嬬粛
JavaMail鐨勬牳蹇冪粍浠朵富瑕佸寘鎷琒ession瀵硅薄鍜孴ransport瀵硅薄銆?/p>
Session瀵硅薄
Session瀵硅薄璐熻矗绠$悊閭欢浼氳瘽锛屽寘鎷缃偖浠舵湇鍔″櫒鐨勫湴鍧€銆佽繛鎺ュ弬鏁般€佽韩浠介獙璇佷俊鎭瓑銆傚畠鏄墍鏈夐偖浠舵搷浣滅殑鍩虹銆傚垱寤轰竴涓猄ession瀵硅薄閫氬父闇€瑕佷娇鐢╜Session.getInstance()`鏂规硶銆傝繕闇€瑕侀厤缃浉鍏崇殑灞炴€э紝濡傞偖浠舵湇鍔″櫒鐨勪富鏈哄悕銆佹槸鍚︿娇鐢⊿SL杩炴帴銆佽韩浠介獙璇佹柟寮忎互鍙婄鍙e彿绛夈€?/p>
Transport瀵硅薄
Transport瀵硅薄鍒欑敤浜庡疄闄呭彂閫侀偖浠躲€傚畠渚濊禆浜嶴ession瀵硅薄锛岄€氳繃session.getTransport()鏂规硶鑾峰彇銆備竴鏃﹁幏鍙栧埌Transport瀵硅薄锛屽氨鍙互浣跨敤瀹冩潵鍙戦€侀偖浠躲€?/p>
浠ヤ笅鏄竴涓畝鍗曠殑绀轰緥锛屽睍绀轰簡濡備綍鍒涘缓涓€涓猄ession瀵硅薄锛?/p>
```java
import javax.mail.Session;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public Session createSession() {
// 璁剧疆閭欢鏈嶅姟鍣ㄧ殑涓绘満鍚?/p>
String host = "smtp.example.com";
// 鏄惁浣跨敤SSL杩炴帴
boolean useSSL = true;
// 浣跨敤榛樿鐨勮韩浠介獙璇佹柟寮?/p>
boolean auth = true;
// 鍒涘缓Properties瀵硅薄鐢ㄤ簬閰嶇疆閭欢鏈嶅姟鍣?/p>
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", String.valueOf(auth));
// 濡傛灉浣跨敤SSL杩炴帴锛岃繕闇€瑕佽缃甋SL绔彛鍙?/p>
if (useSSL) {
props.put("mail.smtp.socketFactory.port", "465");
---
閭欢椋為┌锛氫娇鐢↗avaMail鍙戦€侀偖浠剁殑瀹屽叏鎸囧崡
鍙戦€侀偖浠剁殑鏍稿績娴佺▼
鍦ㄦ暟瀛楀寲閫氳鐨勪笘鐣岄噷锛岀數瀛愰偖浠舵垚涓烘垜浠矡閫氱殑閲嶈妗ユ銆備笅闈㈣鎴戜滑鍊熷姪JavaMail鐨勫姏閲忥紝涓€姝ユ浜嗚В濡備綍鍙戦€侀偖浠躲€?/p>
```java
public void dispatchMail(Session session) {
// 鑾峰彇Transport瀹炰緥锛屽畠鏄偖浠跺彂閫佺殑椹溅
Transport transport = session.getTransport();
// 杩炴帴鑷硈mtp閭欢鏈嶅姟鍣紝濡傚悓椹捐溅鍚▼
transport.connect("smtp.examplecom", "user@examplecom", "password");
// 鎼浇閭欢锛屽彂閫佽嚦鏀朵欢浜?/p>
transport.sendMessage(message, message.getAllRecipients());
// 瀹屾垚浣垮懡锛屽叧闂繛鎺?/p>
transport.close();
}
```
Protocol瀵硅薄鐨勯噸瑕佹€?/p>
鍦ㄩ偖浠剁殑浼犻€掕繃绋嬩腑锛孭rotocol瀵硅薄鎵紨鐫€鑷冲叧閲嶈鐨勮鑹层€傚畠瀹氫箟浜嗛偖浠舵湇鍔″櫒涓庡鎴风涔嬮棿鐨勯€氫俊瑙勫垯锛屽SMTP銆両MAP绛夈€傚湪JavaMail涓紝鎴戜滑寰€寰€閫氳繃Session瀵硅薄鏉ヨ交鏉捐幏鍙栧悎閫傜殑Protocol瀵硅薄銆?/p>
閭欢鏈嶅姟鍣ㄧ殑閰嶇疆涓庢搷浣滅畝杩?/p>
SMTP涓嶪MAP鍗忚鍩虹浠嬬粛
SMTP锛圫imple Mail Transfer Protocol锛夋槸閭欢鍙戦€佺殑鍗忚鍩虹煶锛岃€孖MAP锛圛nternet Message Access Protocol锛夊垯璐熻矗閭欢鐨勬帴鏀朵笌绠$悊銆侸avaMail閫氳繃Protocol瀵硅薄瀹岀編鏀寔杩欎袱绉嶅崗璁€?/p>
閭欢鍙戦€佺殑璇︾粏姝ラ瑙f瀽
1. 閰嶇疆閭欢鏈嶅姟鍣細鏄庣‘SMTP鏈嶅姟鍣ㄧ敤浜庡彂閫侀偖浠讹紝鑰孭OP3鎴朓MAP鏈嶅姟鍣ㄧ敤浜庢帴鏀堕偖浠躲€?/p>
2. 鍒涘缓Session瀹炰緥锛氶€氳繃鎻愪緵鐨刞createSession`鏂规硶锛岃幏鍙栧寘鍚湇鍔″櫒閰嶇疆淇℃伅鐨凷ession瀹炰緥銆?/p>
3. 璁剧疆閭欢淇℃伅锛氱簿蹇冪紪鎺掗偖浠跺唴瀹癸紝鍖呮嫭鍙戜欢浜恒€佹敹浠朵汉銆佷富棰樸€佹鏂囦互鍙婁换浣曢檮浠躲€?/p>
4. 鍙戦€侀偖浠惰嚦鏈嶅姟鍣細閫氳繃Transport瀵硅薄锛屽皢绮惧績鍑嗗鐨勯偖浠跺彂閫佽嚦閭欢鏈嶅姟鍣ㄣ€?/p>
5. 鍝嶅簲涓庡紓甯稿鐞嗭細鍙戦€佸畬閭欢鍚庯紝妫€鏌ュ彂閫佺姸鎬侊紝纭繚閭欢鎴愬姛閫佽揪銆傚噯澶囧ソ闈㈠鍙兘鐨勭綉缁滈棶棰樸€佽韩浠介獙璇佸け璐ョ瓑寮傚父銆?/p>
瀹炶返妗堜緥锛氬彂閫佸甫闄勪欢鐨勯偖浠?/p>
鍦ㄥ疄闄呭簲鐢ㄤ腑锛屾垜浠粡甯搁渶瑕佸彂閫佸甫鏈夐檮浠剁殑閭欢銆備笅闈㈡槸涓€涓畬鏁寸殑绀轰緥锛屽睍绀轰簡濡備綍浣跨敤JavaMail鍙戦€佸甫鏈夐檮浠剁殑閭欢锛?/p>
```java
public void sendAttachedMail(Session session, Message message) {
try {
// 鎼浇闄勪欢锛屽彂閫侀偖浠?/p>
Transport.send(message);
System.out.println("閭欢甯﹂檮浠舵垚鍔熷彂閫?);
} catch (MessagingException e) {
System.out.println("閭欢鍙戦€佸け璐ワ紝鍙兘鐨勫師鍥狅細" + e.getMessage());
e.printStackTrace(); // 鎻愪緵鏇村閿欒淇℃伅锛屼究浜庤皟璇曞拰淇闂
}
}
``` 甯屾湜閫氳繃杩欑瘒鏂囩珷鍜屼唬鐮佺ず渚嬶紝浣犺兘杞绘澗鎺屾彙浣跨敤JavaMail鍙戦€侀偖浠剁殑鎶€宸у拰娴佺▼銆傚湪鏁板瓧鍖栫殑涓栫晫涓紝璁╂矡閫氬彉寰楁洿鍔犱究鎹峰拰楂樻晥锛丣avaMail锛氳交鏉鹃泦鎴愰偖浠跺姛鑳?/p>
鍦↗ava寮€鍙戜腑锛岄泦鎴愰偖浠跺姛鑳藉凡鎴愪负璁稿搴旂敤绋嬪簭鐨勫熀鏈渶姹備箣涓€銆傚浜庤繖绉嶆儏鍐碉紝JavaMail API 鏃犵枒鏄綘鐨勯閫夊伐鍏枫€傜幇鍦ㄨ鎴戜滑鏉ヨ缁嗕簡瑙e浣曚娇鐢ㄥ叾涓殑鍑芥暟鏉ュ疄鐜板甫鏈夐檮浠剁殑閭欢鍙戦€佸姛鑳姐€?/p>
鎴戜滑闇€瑕佸垵濮嬪寲涓€涓偖浠朵細璇濆苟鍑嗗鎵€鏈夊繀瑕佺殑鍙傛暟锛?/p>
```java
public void sendMailWithAttachments(Session session, String sender, String recipient, String subject, String content, String filePath) {
// 鍒涘缓閭欢瀹炰緥
MimeMessage message = new MimeMessage(session);
MimeBodyPart bodyPart = new MimeBodyPart();
DataSource dataSource = new FileDataSource(filePath); // filePath鏄檮浠剁殑璺緞
bodyPart.setDataHandler(new DataHandler(dataSource)); // 璁剧疆闄勪欢鐨勬暟鎹鐞嗗櫒
bodyPart.setFileName(dataSource.getName()); // 璁剧疆闄勪欢鐨勬枃浠跺悕
try {
// 璁剧疆閭欢澶翠俊鎭?/p>
message.setFrom(new InternetAddress(sender)); // 鍙戜欢浜哄湴鍧€
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); // 鏀朵欢浜哄湴鍧€
message.setSubject(subject); // 閭欢涓婚
// 璁剧疆閭欢姝f枃鍐呭
message.setText(content); // 绾枃鏈唴瀹规垨HTML鍐呭鍧囧彲
// 鍒涘缓閭欢涓讳綋骞舵坊鍔犻檮浠堕儴鍒?/p>
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart); // 灏嗛檮浠堕儴鍒嗘坊鍔犲埌閭欢涓讳綋涓?/p>
message.setContent(multipart); // 璁剧疆閭欢鍐呭
// 鍙戦€侀偖浠跺苟杩涜妫€鏌ワ紙鍏蜂綋瀹炵幇渚濊禆浜巗endAndCheckMail鏂规硶锛?/p>
sendAndCheckMail(session, message);
} catch (MessagingException e) { // 澶勭悊鍙兘鍑虹幇鐨勯偖浠跺彂閫佸紓甯?/p>
e.printStackTrace(); // 鎵撳嵃寮傚父淇℃伅锛屾柟渚胯皟璇曞拰鎺掓煡闂
} catch (Exception e) { // 澶勭悊鍏朵粬鍙兘鐨勫紓甯革紝渚嬪鏂囦欢璇诲彇寮傚父绛夛紝瑙嗗叿浣撳疄鐜拌€屽畾銆?}
}
```鎺ヤ笅鏉ユ槸瀵瑰涔燡avaMail鐨勪竴浜涘缓璁笌璧勬簮鎺ㄨ崘锛?馃摟 JavaMail鐨勬牳蹇冪粍浠朵笌浣跨敤鏂瑰紡锛氭帉鎻avaMail鐨勫熀纭€鍔熻兘鏄疄鐜伴偖浠惰嚜鍔ㄥ寲鐨勫叧閿€傞€氳繃浜嗚В鍏舵牳蹇冪粍浠跺MimeMessage銆丮imeBodyPart绛夛紝浣犲彲浠ヨ交鏉鹃泦鎴愰偖浠跺姛鑳藉埌鍚勭Java搴旂敤涓€?馃摎 瀹樻柟鏂囨。涓庣ぞ鍖烘暀绋嬫帹鑽愶細鎯宠娣卞叆浜嗚ВJavaMail鐨勯珮绾у簲鐢ㄥ拰鍔熻兘鐗规€э紝鍙互鍙傝€冨叾瀹樻柟鏂囨。鍜岀ぞ鍖烘暀绋嬨€侸avaMail瀹樻柟鏂囨。鏄綘鎺㈢储杩欎釜寮哄ぇ宸ュ叿搴撶殑绗竴姝ャ€傚湪绾跨紪绋嬪涔犲钩鍙板鎱曡缃戜笂鐨勭浉鍏宠绋嬩篃鏄綘杩涢樁瀛︿範鐨勮壇濂介€夋嫨銆?馃挭 瀹炶返涓庢寔缁涔犵殑閲嶈鎬э細瑕佹兂鏇村ソ鍦板埄鐢↗avaMail鏋勫缓楂樻晥銆佸彲闈犵殑閭欢绯荤粺锛屾弧瓒充笉鏂彉鍖栫殑搴旂敤鍦烘櫙闇€姹傦紝浣犻渶瑕佷笉鏂湴瀹炶返骞舵繁鍏ュ涔犵浉鍏虫妧鏈€傞€氳繃瀹為檯鐨勫紑鍙戠粡楠屽拰鎸佺画鐨勫涔狅紝浣犲皢鑳藉鏇存繁鍏ュ湴鐞嗚В鍜屾帉鎻avaMail鐨勪娇鐢ㄦ妧宸э紝浠庤€屼负浣犳湭鏉ョ殑椤圭洰甯︽潵鏇村ぇ鐨勪环鍊笺€?JavaMail鏄鐞嗛偖浠朵换鍔$殑寮哄ぇ宸ュ叿銆傞€氳繃鎺屾彙鍏跺熀纭€鐭ヨ瘑鍜岄珮绾у簲鐢紝浣犲彲浠ヨ交鏉惧湴灏嗛偖浠跺姛鑳介泦鎴愬埌浣犵殑搴旂敤绋嬪簭涓紝婊¤冻鍚勭鑷姩鍖栭€氱煡銆佹姤鍛婂彂閫佺瓑闇€姹傘€?/p>
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。