欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaMail實(shí)現(xiàn)發(fā)送郵件(QQ郵箱)

 更新時(shí)間:2022年08月11日 10:26:52   作者:失落的葉  
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)發(fā)送郵件(QQ郵箱),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaMail實(shí)現(xiàn)發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下

用的qq郵箱,需要去郵箱設(shè)置那邊開(kāi)一下stmp服務(wù)啥的獲得下面要用到的密碼,具體開(kāi)服務(wù)自己百度,這邊不截圖了。

代碼如下:導(dǎo)包和工具類(lèi),可用!

一、導(dǎo)這個(gè)包

<dependency>
? ? <groupId>javax.mail</groupId>
? ? ?<artifactId>mail</artifactId>
? ? <version>1.5.0-b01</version>
</dependency>

二、工具類(lèi)封裝成對(duì)象

import lombok.Data;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailHelper
{
? ? /**
? ? ?* 郵件服務(wù)器主機(jī)名。
? ? ?*/
? ? private static String HOST_NAME;

? ? private String sendMailUrl ;
? ? private String receiveMailUrl;
? ? /**
? ? ?* IMAP/SMTP服務(wù)的密碼 去qq郵箱開(kāi)的。 幾個(gè)月前的失效了還是蠻奇怪的 2021年8月5日21:33:36
? ? ?*/
? ? private String servicePassword;


? ? static {
? ? ? ? //默認(rèn)直接qq的吧
? ? ? ? HOST_NAME = "smtp.qq.com";
? ? }
? ? public MailHelper(String sendMailUrl,String receiveMailUrl,String servicePassword){
? ? ? ? this.sendMailUrl=sendMailUrl;
? ? ? ? this.receiveMailUrl=receiveMailUrl;
? ? ? ? this.servicePassword=servicePassword;
? ? }

? ? public ?void sendSimpleMail(Mail mail) throws Exception {
? ? ? ? Properties prop = new Properties();
? ? ? ? ?設(shè)置郵件服務(wù)器主機(jī)名
? ? ? ? prop.setProperty("mail.host", "smtp.qq.com");
? ? ? ? // 發(fā)送郵件協(xié)議名稱(chēng)
? ? ? ? prop.setProperty("mail.transport.protocol", "smtp");
? ? ? ? prop.setProperty("mail.smtp.auth", "true");
? ? ? ? // 使用JavaMail發(fā)送郵件的5個(gè)步驟
? ? ? ? // 1、創(chuàng)建session 根據(jù)配置創(chuàng)建會(huì)話(huà)對(duì)象, 用于和郵件服務(wù)器交互
? ? ? ? Session session = Session.getInstance(prop);
? ? ? ? // 開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
? ? ? ? session.setDebug(true);
? ? ? ? // 2、通過(guò)session得到transport對(duì)象
? ? ? ? Transport ts = session.getTransport();
? ? ? ? // 3、使用郵箱的用戶(hù)名和密碼連上郵件服務(wù)器,發(fā)送郵件時(shí),發(fā)件人需要提交郵箱的用戶(hù)名和密碼給smtp服務(wù)器,用戶(hù)名和密碼都通過(guò)驗(yàn)證之后才能夠正常發(fā)送郵件給收件人。

? ? ? ? // 注:這邊host必須填寫(xiě)smtp.qq.com
? ? ? ? // 而不是你qq郵箱賬號(hào)如1741049@qq.com,否則報(bào)錯(cuò)host名unkonwn。
? ? ? ? // Host:郵件服務(wù)器主機(jī)名
? ? ? ? ts.connect(HOST_NAME, receiveMailUrl, servicePassword);
? ? ? ? // 4、創(chuàng)建郵件
? ? ? ? Message message = createSimpleMail(session, mail.getTitle(), mail.getContent());
? ? ? ? // 5、發(fā)送郵件
? ? ? ? ts.sendMessage(message, message.getAllRecipients());
? ? ? ? ts.close();
? ? }
? ??
? ? private MimeMessage createSimpleMail(Session session, String title, String content) throws Exception {
? ? ? ? // 創(chuàng)建郵件對(duì)象
? ? ? ? MimeMessage message = new MimeMessage(session);
? ? ? ? // 指明郵件的發(fā)件人
? ? ? ? message.setFrom(new InternetAddress(receiveMailUrl));
? ? ? ? // 指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā)
? ? ? ? message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailUrl));
? ? ? ? // 郵件的標(biāo)題
? ? ? ? message.setSubject(title);
? ? ? ? // 郵件的文本內(nèi)容
? ? ? ? message.setContent(content, "text/html;charset=UTF-8");
? ? ? ? // 返回創(chuàng)建好的郵件對(duì)象
? ? ? ? return message;
? ? }
? ? @Data
? ? public static class Mail{
? ? ? ? private String title;
? ? ? ? /**
? ? ? ? ?* 正文
? ? ? ? ?*/
? ? ? ? private String content;
? ? ? ? public Mail(){

? ? ? ? }
? ? ? ? public Mail(String title,String content){
? ? ? ? ? ? this.title=title;
? ? ? ? ? ? this.content=content;
? ? ? ? }

? ? }
}

測(cè)試類(lèi):

package com.forever.junittest;

import com.forever.gitfund.util.MailHelper;
import com.forever.gitfund.util.MailHelper.Mail;
import org.junit.Test;

public class TestMailHelper {

? ? @Test
? ? public void test() throws Exception {
? ? ? ? String qq = "xx@qq.com";
? ? ? ? String send = qq;
? ? ? ? MailHelper mailHelper = new MailHelper(qq,send,"xxxxx");
? ? ? ? Mail mail = new Mail();
? ? ? ? String title = "我的第一封郵件";
? ? ? ? String content = "這是我的第一封郵件 from idea";
? ? ? ? mail.setTitle(title);
? ? ? ? mail.setContent(content);
? ? ? ? mailHelper.sendSimpleMail(mail);
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論