JavaMail入门:轻松构建邮件发送系统

当前位置: 钓虾网 > 圈子 > JavaMail入门:轻松构建邮件发送系统

JavaMail入门:轻松构建邮件发送系统

2024-11-08 作者:钓虾网 1

JavaMail锛欽ava鐨勯偖浠跺鐞咥PI璇﹁В

JavaMail入门:轻松构建邮件发送系统

JavaMail鏄竴涓紑婧愮殑Java API锛岀敤浜庡鐞嗛偖浠剁殑鍙戦€佸拰鎺ユ敹锛岃鍖呭惈鍦↗avaMail/JSR 204椤圭洰涓€傚畠涓篔ava寮€鍙戣€呮彁渚涗簡涓€缁勪赴瀵岀殑API锛屾敮鎸丼MTP銆丳OP3銆両MAP绛夊绉嶉偖浠跺崗璁紝鑳借交鏉惧疄鐜伴偖浠剁殑鍙戦€併€佹帴鏀躲€佸鐞嗙瓑鎿嶄綔銆傛帴涓嬫潵锛岃鎴戜滑娣卞叆浜嗚ВJavaMail鐨勫伐浣滃師鐞嗭紝骞堕€氳繃瀹為檯绀轰緥鏋勫缓涓€涓偖浠跺彂閫佺郴缁熴€?/p>

JavaMail入门:轻松构建邮件发送系统

搴旂敤棰嗗煙骞挎硾

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》整理于网络,文章内容不代表本站立场,转载请注明出处。

本文链接:https://www.jnqjk.cn/quanzi/163793.html

AI推荐

Copyright 2024 © 钓虾网 XML

蜀ICP备2022021333号-1