日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

JavaMail 发送邮件

發(fā)布時間:2025/7/14 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaMail 发送邮件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JavaMail郵件發(fā)送

引用maven jar包

1 <dependency> 2 <groupId>javax.mail</groupId> 3 <artifactId>mail</artifactId> 4 <version>1.4.5</version> 5 </dependency>

?

發(fā)送郵件函數(shù)

private static void sendMail(){// 配置發(fā)送郵件的環(huán)境屬性final Properties props = new Properties();/** 可用的屬性: mail.store.protocol / mail.transport.protocol / mail.host /* mail.user / mail.from*/// 表示SMTP發(fā)送郵件,需要進行身份驗證props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", "smtp.163.com");// 發(fā)件人的賬號props.put("mail.user", "*****@163.com");// 訪問SMTP服務時需要提供的密碼props.put("mail.password", "******");// 構建授權信息,用于進行SMTP進行身份驗證Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用戶名、密碼String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用環(huán)境屬性和授權信息,創(chuàng)建郵件會話Session mailSession = Session.getInstance(props, authenticator);// 創(chuàng)建郵件消息MimeMessage message = new MimeMessage(mailSession);try {// 設置發(fā)件人InternetAddress form = null;form = new InternetAddress(props.getProperty("mail.user"));message.setFrom(form);// 設置收件人InternetAddress to = new InternetAddress(****@163.com);message.setRecipient(Message.RecipientType.TO, to);// 設置郵件標題message.setSubject("測試郵件");// 設置郵件的內(nèi)容體message.setContent("<a href='http://www.cnblogs.com/dawnheaven/'>測試的HTML郵件</a>", "text/html;charset=UTF-8");// 發(fā)送郵件 Transport.send(message);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}

?

?

轉載于:https://www.cnblogs.com/dawnheaven/p/5057150.html

總結

以上是生活随笔為你收集整理的JavaMail 发送邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。