生活随笔
收集整理的這篇文章主要介紹了
Java-使用JavaMail发送一封邮件给用户邮箱功能实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<—————————–start—————————–> 通過javamail發(fā)送一封郵件: 較早前,我已經(jīng)編寫好了發(fā)送郵件的工具類MailUtils。有以下要點(diǎn)需要注意: ① 發(fā)件箱的郵件服務(wù)器地址。 ② 發(fā)件箱賬號。 ③ 郵箱授權(quán)碼,在所使用的郵箱后臺管理中設(shè)置。 ④ 激活郵件的地址。
public class MailUtils {
private static String smtp_host =
"smtp.163.com" ;
private static String username =
"niwotaxuexiba_search@163.com" ;
private static String password =
"niwotaxuexiba123" ;
private static String
from =
"niwotaxuexiba_search@163.com" ;
public static String activeUrl =
"http://localhost:9003/bos_fore/customer_activeMail" ;
public static void sendMail (String subject, String content, String to) {Properties props =
new Properties();props.setProperty(
"mail.smtp.host" , smtp_host);props.setProperty(
"mail.transport.protocol" ,
"smtp" );props.setProperty(
"mail.smtp.auth" ,
"true" );Session session = Session.getInstance(props);Message message =
new MimeMessage(session);
try {message.setFrom(
new InternetAddress(
from ));message.setRecipient(RecipientType.TO,
new InternetAddress(to));message.setSubject(subject);message.setContent(content,
"text/html;charset=utf-8" );Transport transport = session.getTransport();transport.connect(smtp_host, username, password);transport.sendMessage(message, message.getAllRecipients());}
catch (Exception e) {e.printStackTrace();
throw new RuntimeException(
"郵件發(fā)送失敗..." );}}
public static void main (String[] args) {sendMail(
"測試郵件" ,
"你好,你我他學(xué)習(xí)吧" ,
"niwotaxuexiba_search@163.com" );}
}
在sendMail(參數(shù)①,參數(shù)②,參數(shù)③)方法中設(shè)置三個(gè)參數(shù): ① 郵件主題; ② 郵件內(nèi)容; ③ 目標(biāo)郵箱賬戶(發(fā)給誰)。 需要在pom.xml文件中添加javamail郵件發(fā)送工具包的坐標(biāo)。添加完依賴后重新install以下。 <—————————–end—————————–>
與50位技術(shù)專家面對面 20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔 為你收集整理的Java-使用JavaMail发送一封邮件给用户邮箱功能实现 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。