通過java進程發(fā)送電子郵件是一個簡單且容易實現(xiàn)的過程。該過程是一個即時過程,基于java的電子郵件過程的兩個必備項是JavamailAPI和JAF框架。這是用 java 發(fā)送電子郵件的兩個主要部分。這些部分使基于 Java 的應用程序中的電子郵件發(fā)送過程變得更加簡單。 Java郵件API和JAF都可以從java標準網(wǎng)站下載。 smtp 服務器也可用于發(fā)送電子郵件。此 SMTP 服務器用法是電子郵件生成的替代方法。安裝和使用 SMTP 服務器(例如播客服務器和 apache James 服務器)是另一種方法。
開始您的免費軟件開發(fā)課程
網(wǎng)絡開發(fā)、編程語言、軟件測試及其他
Java 發(fā)送電子郵件的步驟
發(fā)送郵件的關鍵步驟如下,
1) 檢索會話對象。
2) 撰寫要發(fā)送的消息。
3) 發(fā)送消息。
讓我們詳細討論每個步驟,檢索會話對象的第一步負責拉取基于會話的對象。創(chuàng)建的每個會話都可能有一個與其關聯(lián)的對象。這些對象將與會話相關的信息緊密耦合。為了檢索與會話相對應的對象,javax.需要使用mail.Session類。此類有兩種不同的方法用于檢索對象實例詳細信息。因此,有兩個內置方法用于檢索對象實例詳細信息:Session。 getdefaultinstance() 方法和 Session. getinstance() 方法。這是提取關聯(lián)對象詳細信息的兩個關鍵方法。要檢索會話對象本身,可以使用以下任何方法來處理這種情況,
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 |
撰寫消息:這是此過程中需要考慮的非常關鍵的步驟。該步驟涉及從源頭制定原始預期消息的過程。因此,由于本節(jié)涉及原始消息,因此這是需要考慮的非常關鍵的部分。為了發(fā)生堆肥過程,使用了 javax.mail.message。此類允許復雜地構建消息。該類處于操作的抽象級別,因此其子類稱為 javax.mail.internet.MimeMessage 更專門用于此過程。會話和關聯(lián)的消息將使用以下代碼段進行堆積。因此,此代碼用于組合消息和會話詳細信息 MimeMessage message=new MimeMessage(session);
發(fā)送消息:本節(jié)的最后一個過程是發(fā)送消息。 javax.郵件。傳輸類就是用于此目的。該類的目的是觸發(fā)發(fā)送消息的過程。所以具體來說,發(fā)送消息的過程可以通過javax.具體是mail.transport消息。從編碼的角度來看,Transport 的代碼片段。發(fā)送(消息);正是用于此過程。
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(); } } }
輸出:
JavaMail 在電子郵件中發(fā)送附件
代碼:
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 電子郵件消息以及設置電子郵件消息涉及哪些類,還描述了用于發(fā)送電子郵件以及發(fā)送附有文件的電子郵件的程序。
以上是Java電子郵件的詳細內容。更多信息請關注PHP中文網(wǎng)其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

Laravel支持使用原生SQL查詢,但應優(yōu)先使用參數(shù)綁定以確保安全;1.使用DB::select()執(zhí)行帶參數(shù)綁定的SELECT查詢,防止SQL注入;2.使用DB::update()執(zhí)行UPDATE操作并返回影響行數(shù);3.使用DB::insert()插入數(shù)據(jù);4.使用DB::delete()刪除數(shù)據(jù);5.使用DB::statement()執(zhí)行如CREATE、ALTER等無結果集的SQL語句;6.推薦在QueryBuilder中使用whereRaw、selectRaw等方法結合原生表達式以提升安

使用JUnit5和Mockito能有效隔離依賴進行單元測試,1.通過@Mock創(chuàng)建模擬對象,@InjectMocks注入被測實例,@ExtendWith啟用Mockito擴展;2.使用when().thenReturn()定義模擬行為,verify()驗證方法調用次數(shù)與參數(shù);3.可模擬異常場景并驗證錯誤處理;4.推薦構造函數(shù)注入、避免過度模擬、保持測試原子性;5.使用assertAll()合并斷言,@Nested組織測試場景,從而提升測試可維護性和可靠性。

Go泛型從1.18開始支持,用于編寫類型安全的通用代碼。1.泛型函數(shù)PrintSlice[Tany](s[]T)可打印任意類型切片,如[]int或[]string。2.通過類型約束Number限制T為int、float等數(shù)字類型,實現(xiàn)Sum[TNumber](slice[]T)T安全求和。3.泛型結構體typeBox[Tany]struct{ValueT}可封裝任意類型值,配合NewBox[Tany](vT)*Box[T]構造函數(shù)使用。4.為Box[T]添加Set(vT)和Get()T方法,無需

table-layout:fixed會強制表格列寬由第一行單元格寬度決定,避免內容影響布局。1.設置table-layout:fixed并指定表格寬度;2.為第一行th/td設置具體列寬比例;3.配合white-space:nowrap、overflow:hidden和text-overflow:ellipsis控制文本溢出;4.適用于后臺管理、數(shù)據(jù)報表等需穩(wěn)定布局和高性能渲染的場景,能有效防止布局抖動并提升渲染效率。

json.loads()用于將JSON字符串解析為Python數(shù)據(jù)結構,1.輸入必須是雙引號包裹的字符串且布爾值為true/false;2.支持null→None、對象→dict、數(shù)組→list等自動轉換;3.常用于處理API返回的JSON字符串,如response_string經json.loads()解析后可直接訪問嵌套數(shù)據(jù),使用時需確保JSON格式正確,否則會拋出異常。

Choosetheappropriateindextypebasedonusecase,suchassinglefield,compound,multikey,text,geospatial,orTTLindexes.2.ApplytheESRrulewhencreatingcompoundindexesbyorderingfieldsasequality,sort,thenrange.3.Designindexestosupportcoveredqueriesbyincludingallque

Maven是Java項目管理和構建的標準工具,答案在于它通過pom.xml實現(xiàn)項目結構標準化、依賴管理、構建生命周期自動化和插件擴展;1.使用pom.xml定義groupId、artifactId、version和dependencies;2.掌握核心命令如mvnclean、compile、test、package、install和deploy;3.利用dependencyManagement和exclusions管理依賴版本與沖突;4.通過多模塊項目結構組織大型應用并由父POM統(tǒng)一管理;5.配

Python中函數(shù)傳參是“傳遞對象引用”,即1.對于可變對象(如列表、字典),函數(shù)內進行原地修改(如append、賦值切片)會直接影響原對象;2.對于不可變對象(如整數(shù)、字符串),函數(shù)內無法改變原對象,重新賦值只會創(chuàng)建新對象;3.參數(shù)傳遞的是引用的副本,若在函數(shù)內重新綁定變量(如lst=[...]),則斷開與原對象的連接,不影響外部變量。因此,修改可變對象會影響原數(shù)據(jù),而不可變對象和重新賦值則不會,這解釋了為何列表在函數(shù)內修改后外部可見,而整數(shù)變化僅限局部。
