更新時間:2022-08-16 08:48:32 來源:動力節點 瀏覽1375次
使用您的 Java 應用程序發送電子郵件非常簡單,但首先您應該在您的機器上安裝JavaMail API和Java 激活框架 (JAF) 。
您可以從 Java 的標準網站下載最新版本的JavaMail(1.2 版) 。
您可以從 Java 的標準網站下載最新版本的JAF(版本 1.1.1) 。
下載并解壓縮這些文件,在新創建的頂級目錄中,您會發現兩個應用程序的許多 jar 文件。您需要在 CLASSPATH中添加mail.jar和activation.jar文件。
這是一個從您的機器發送簡單電子郵件的示例。假設您的localhost已連接到 Internet 并且有足夠的能力發送電子郵件。
例子
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String [] args) {
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運行這個程序來發送一封簡單的電子郵件
輸出
$ java SendEmail
Sent message successfully....
如果您想向多個收件人發送電子郵件,則將使用以下方法指定多個電子郵件 ID
void addRecipients(Message.RecipientType type, Address[] addresses)
throws MessagingException
這是參數的描述
type - 這將設置為 TO、CC 或 BCC。這里CC代表復本,BCC代表黑復本。示例:Message.RecipientType.TO
地址- 這是一個電子郵件 ID 數組。您需要在指定電子郵件 ID 時使用 InternetAddress() 方法。
這是從您的機器發送 HTML 電子郵件的示例。這里假設您的localhost已連接到 Internet 并且有足夠的能力發送電子郵件。
這個例子和上一個非常相似,除了這里我們使用 setContent() 方法來設置內容,其第二個參數是“text/html”來指定 HTML 內容包含在消息中。
使用此示例,您可以發送任意大小的 HTML 內容。
例子
// File Name SendHTMLEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail {
public static void main(String [] args) {
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>This is actual message</h1>", "text/html");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運行此程序以發送 HTML 電子郵件
輸出
$ java SendHTMLEmail
Sent message successfully....
這是從您的機器發送帶有附件的電子郵件的示例。這里假設您的localhost已連接到 Internet 并且有足夠的能力發送電子郵件。
例子
// File Name SendFileEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail {
public static void main(String [] args) {
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
編譯并運行此程序以發送 HTML 電子郵件
輸出
$ java SendFileEmail
Sent message successfully....
如果需要向電子郵件服務器提供用戶 ID 和密碼以進行身份??驗證,那么您可以將這些屬性設置如下
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
其余的電子郵件發送機制將保持如上所述。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習