原文位址: http://www.work100.net/training/java-email.html 更多教程: 光束雲 - 免費課程
郵件收發
序号 | 文内章節 | 視訊 |
---|---|---|
1 | 概述 | - |
2 | 發送一封簡單的郵件 | |
3 | 發送一封HTML郵件 | |
4 | 發送帶有附件的郵件 | |
5 | 使用者認證 |
請參照如上
章節導航
進行閱讀
1.概述
使用Java應用程式發送 E-mail 十分簡單,但是首先你應該在你的機器上安裝
JavaMail API
和
Java Activation Framework
(
JAF
) 。
- 您可以從 Java 網站下載下傳最新版本的
,打開網頁右側有個JavaMail
連結,點選它下載下傳Downloads
-
(版本 1.1.1)JAF
你也可以使用本站提供的下載下傳連結:
- JavaMail
mail.jar 1.4.5
- JAF(版本 1.1.1)
activation.jar
下載下傳并解壓縮這些檔案,在新建立的頂層目錄中,您會發現這兩個應用程式的一些
jar
檔案。您需要把
mail.jar
activation.jar
檔案添加到您的
CLASSPATH
中。
如果你使用第三方郵件伺服器如
QQ
的
SMTP
伺服器,可檢視文章底部使用者認證完整的執行個體。
2.發送一封簡單的郵件
下面是一個發送簡單
E-mail
的例子。假設你的本地主機已經連接配接到網絡。
// 檔案名 SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// 收件人電子郵箱
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定發送郵件的主機為 localhost
String host = "localhost";
// 擷取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
// 擷取預設session對象
Session session = Session.getDefaultInstance(properties);
try{
// 建立預設的 MimeMessage 對象
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("This is the Subject Line!");
// 設定消息體
message.setText("This is actual message");
// 發送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運作這個程式來發送一封簡單的
E-mail
:
$ java SendEmail
Sent message successfully....
如果你想發送一封
E-mail
給多個收件人,那麼使用下面的方法來指定多個
收件人ID
void addRecipients(Message.RecipientType type,
Address[] addresses)
throws MessagingException
下面是對于參數的描述:
- type: 要被設定為
,TO
或者CC
,這裡BCC
代表抄送、CC
代表秘密抄送。舉例:BCC
Message.RecipientType.TO
- addresses: 這是
的數組。在指定電子郵件 ID 時,你将需要使用email ID
方法。InternetAddress()
3.發送一封HTML郵件
下面是一個發送
HTML E-mail
和上一個例子很相似,除了我們要使用
setContent()
方法來通過第二個參數為 "
text/html
",來設定内容來指定要發送
HTML
内容。
// 檔案名 SendHTMLEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail
{
public static void main(String [] args)
{
// 收件人電子郵箱
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定發送郵件的主機為 localhost
String host = "localhost";
// 擷取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
// 擷取預設的 Session 對象。
Session session = Session.getDefaultInstance(properties);
try{
// 建立預設的 MimeMessage 對象。
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("This is the Subject Line!");
// 發送 HTML 消息, 可以插入html标簽
message.setContent("<h1>This is actual message</h1>",
"text/html" );
// 發送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運作此程式來發送
HTML e-mail
$ java SendHTMLEmail
Sent message successfully....
4.發送帶有附件的郵件
下面是一個發送帶有附件的
E-mail
// 檔案名 SendFileEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail
{
public static void main(String [] args)
{
// 收件人電子郵箱
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定發送郵件的主機為 localhost
String host = "localhost";
// 擷取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
// 擷取預設的 Session 對象。
Session session = Session.getDefaultInstance(properties);
try{
// 建立預設的 MimeMessage 對象。
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("This is the Subject Line!");
// 建立消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("This is message body");
// 建立多重消息
Multipart multipart = new MimeMultipart();
// 設定文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 發送完整消息
message.setContent(multipart );
// 發送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運作你的程式來發送一封帶有附件的郵件。
$ java SendFileEmail
Sent message successfully....
5.使用者認證
如果需要提供使用者名和密碼給
e-mail
伺服器來達到使用者認證的目的,你可以通過如下設定來完成:
props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
e-mail其他的發送機制和上述保持一緻。
需要使用者名密碼驗證郵件發送執行個體:
本執行個體以 QQ 郵件伺服器為例,你需要在登入QQ郵箱背景在
設定
=》
賬号
=》 開啟
POP3/SMTP
服務 ,如下圖所示:

QQ 郵箱通過生成授權碼來設定密碼:
Java 代碼如下:
// 需要使用者名密碼郵件發送執行個體
//檔案名 SendEmail2.java
//本執行個體以QQ郵箱為例,你需要在qq背景設定
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail2
{
public static void main(String [] args)
{
// 收件人電子郵箱
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定發送郵件的主機為 smtp.qq.com
String host = "smtp.qq.com"; //QQ 郵件伺服器
// 擷取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
// 擷取預設session對象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "qq郵箱授權碼"); //發件人郵件使用者名、授權碼
}
});
try{
// 建立預設的 MimeMessage 對象
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("This is the Subject Line!");
// 設定消息體
message.setText("This is actual message");
// 發送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
設定SSL加密
關于
QQ郵箱
,還要設定
SSL
加密,加上以下代碼即可
代碼如下:
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
完整參考代碼:
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendEmail
{
public static void main(String [] args) throws GeneralSecurityException
{
// 收件人電子郵箱
String to = "[email protected]";
// 發件人電子郵箱
String from = "[email protected]";
// 指定發送郵件的主機為 smtp.qq.com
String host = "smtp.qq.com"; //QQ 郵件伺服器
// 擷取系統屬性
Properties properties = System.getProperties();
// 設定郵件伺服器
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);
// 擷取預設session對象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "授權的 QQ 郵箱密碼"); //發件人郵件使用者名、密碼
}
});
try{
// 建立預設的 MimeMessage 對象
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("This is the Subject Line!");
// 設定消息體
message.setText("This is actual message");
// 發送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
上一篇:
網絡程式設計下一篇:
多線程如果對課程内容感興趣,可以掃碼關注我們的或
公衆号
,及時關注我們的課程更新
QQ群