首頁  >  文章  >  Java  >  Java電子郵件

Java電子郵件

王林
王林原創
2024-08-30 16:21:59792瀏覽

透過java進程發送電子郵件是一個簡單且容易實現的過程。這個過程是一個即時過程,基於java的電子郵件過程的兩個必備項是JavamailAPI和JAF框架。這是用 java 發送電子郵件的兩個主要部分。這些部分使基於 Java 的應用程式中的電子郵件發送過程變得更加簡單。 Java郵件API和JAF都可以從java標準網站下載。 smtp 伺服器也可用於傳送電子郵件。此 SMTP 伺服器用法是電子郵件產生的替代方法。安裝和使用 SMTP 伺服器(例如播客伺服器和 apache James 伺服器)是另一種方法。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java 發送電子郵件的步驟

發送郵件的關鍵步驟如下,

1) 檢索會話物件。
2) 撰寫要發送的訊息。
3) 發送訊息。

讓我們詳細討論每個步驟,檢索會話物件的第一步負責拉取基於會話的物件。創建的每個會話都可能有一個與其關聯的物件。這些物件將與會話相關的資訊緊密耦合。為了檢索與會話相對應的對象,javax.需要使用mail.Session類別。此類有兩種不同的方法用於檢索物件實例詳細資訊。因此,有兩個內建方法用於檢索物件實例詳細資訊:Session。 getdefaultinstance() 方法和 Session. getinstance() 方法。這是提取關聯對象詳細資訊的兩個關鍵方法。要檢索會話物件本身,可以使用以下任何方法來處理這種情況,

s.no Method details Description
1 public static Session getDefaultInstance(Properties p) default session value will be returned
2 public static Session getDefaultInstance(Properties p,Authenticator a) default session value will be returned
3 public static Session getInstance(Properties prop) Value associated to the new session will be returned
4 public static Session getInstance(Properties prop,Authenticator a) Value associated to the new session will be returned

撰寫訊息:這是此過程中需要考慮的非常關鍵的步驟。此步驟涉及從源頭制定原始預期訊息的過程。因此,由於本節涉及原始訊息,因此這是需要考慮的非常關鍵的部分。為了發生堆肥過程,使用了 javax.mail.message。此類別允許複雜地建構訊息。該類別處於操作的抽象級別,因此其子類別稱為 javax.mail.internet.MimeMessage 更專門用於此過程。會話和關聯的訊息將使用以下程式碼段進行堆積。因此,此程式碼用於組合訊息和會話詳細資料 MimeMessage message=new MimeMessage(session);
發送訊息:本節的最後一個過程是發送訊息。 javax.郵件。傳輸類別就是用於此目的。該類別的目的是觸發發送訊息的過程。所以具體來說,發送訊息的過程可以透過javax.具體是mail.transport訊息。從編碼的角度來看,Transport 的程式碼片段。發送(訊息);正是用於此過程。

No. Method Description
1 public static void send(Message m) The given method is used for sending the message. So transport of the message can be achieved by means of this method.
2 public static void send(Message m, Address[] address) For sending the message to one specific address this method is used.

Javax 郵件程式步驟

代碼:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class Initiateemail {
public static void main(String [] args) {
// email id of the recipient has to be mentioned in this field
String to = "[email protected]";
// email id of the sender needs to be mentioned here
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// All details associated to the property are mentioned here
Properties prop = System.getProperties();
// this is the step were the property setup can be eastablished
prop.setProperty("mail.smtp.host", host);
Session ses = Session.getDefaultInstance(prop);
try {
// onject associated to the message is initiated here
MimeMessage mess = new MimeMessage(ses);
// header details are decided and set here.
mes.setFrom(new InternetAddress(from));
// header field details are created here
mes.addRecipient(Mes.RecipientType.TO, new InternetAddress(to));
// subject details of the message are given here
mes.setSubject("Hello world message . . . . . . . . . . . subject starts ");
// Actual message of the email is given here
message.setText("Hello world . . . . . . . . . . . . . . . Message Ends");
// transport object is used for initiating the message
Transport.send(mes);
System.out.println("message has been sent successfully . . . . . ");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

輸出:

Java電子郵件

Java電子郵件

JavaMail 在電子郵件中傳送附件

代碼:

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]";
String host = "localhost";
Properties prop = System.getProperties();
prop.setProperty("mail.smtp.host", host);
Session ses = Session.getDefaultInstance(prop);
try {
MimeMessage mess = new MimeMessage(ses);
mes.setFrom(new InternetAddress(from));
mes.addRecipient(Mes.RecipientType.TO, new InternetAddress(to));
mes.setSubject("Hello world message . . . . . . . . . . . subject starts ");
message.setText("Hello world . . . . . . . . . . . . . . . Message Ends");
mp.addBodyPart(mbp);
mbp = new MimeBodyPart();
String fl = "newfile.txt";
DataSource src= new FileDataSource(fl);
mbp.setDataHandler(new DataHandler(src));
mbp.setFileName(filename);
mp.addBodyPart(mbp);
messetContent(mp );
Transport.send(mes);
System.out.println("message has been sent successfully . . . . . ");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

輸出:

Java電子郵件

Java電子郵件

結論

本文解釋瞭如何設定 java 電子郵件訊息以及設定電子郵件訊息涉及哪些類,還描述了用於發送電子郵件以及發送附有文件的電子郵件的程式。

以上是Java電子郵件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn