新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
java如何發(fā)送html郵件
要使用Java發(fā)送HTML郵件,可以使用JavaMail API,以下是詳細步驟:

成都創(chuàng)新互聯(lián)長期為千余家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為秦安企業(yè)提供專業(yè)的網(wǎng)站設計制作、網(wǎng)站制作,秦安網(wǎng)站改版等技術服務。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
1、添加JavaMail依賴
在項目的pom.xml文件中添加JavaMail的依賴:
com.sun.mail javax.mail 1.6.2
2、編寫Java代碼
創(chuàng)建一個Java類,如HtmlEmailSender.java,并編寫以下代碼:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class HtmlEmailSender {
public static void main(String[] args) {
// 設置郵件服務器屬性
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.example.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// 創(chuàng)建郵件會話
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication("[email protected]", "your_password");
}
});
try {
// 創(chuàng)建郵件消息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("HTML郵件示例");
// 設置郵件內(nèi)容為HTML格式
message.setContent("歡迎使用Java發(fā)送HTML郵件!
| 姓名 | 年齡 |
|---|---|
| 張三 | 30 |
注意替換[email protected]、your_password和[email protected]為實際的發(fā)件人郵箱、密碼和收件人郵箱。
3、運行Java程序
運行HtmlEmailSender.java,如果一切正常,收件人將收到一封包含HTML內(nèi)容的郵件。
本文題目:java如何發(fā)送html郵件
文章分享:http://m.fisionsoft.com.cn/article/cdjpojg.html


咨詢
建站咨詢
