java實現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例
下載和上傳附件、發(fā)送短信和發(fā)送郵件,都算是程序中很常用的功能,之前記錄了文件的上傳和下載還有發(fā)送短信,由于最近比較忙,郵件發(fā)送的功能就沒有時間去弄,現(xiàn)在終于成功以163郵箱發(fā)送郵件到qq郵箱,以下是相關(guān)代碼,具體解釋可以參考代碼中注釋:
package test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.sun.mail.util.MailSSLSocketFactory;
///**
// *
// * @author tuzongxun123
// * @Description 郵件發(fā)送測試類
// */
public class sendMailTest {
public static void main(String[] args) throws Exception {
// 配置信息
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.163.com");
pro.put("mail.smtp.auth", "true");
// SSL加密
MailSSLSocketFactory sf = null;
sf = new MailSSLSocketFactory();
// 設(shè)置信任所有的主機(jī)
sf.setTrustAllHosts(true);
pro.put("mail.smtp.ssl.enable", "true");
pro.put("mail.smtp.ssl.socketFactory", sf);
// 根據(jù)郵件的會話屬性構(gòu)造一個發(fā)送郵件的Session,這里需要注意的是用戶名那里不能加后綴,否則便不是用戶名了
//還需要注意的是,這里的密碼不是正常使用郵箱的登陸密碼,而是客戶端生成的另一個專門的授權(quán)碼
MailAuthenticator authenticator = new MailAuthenticator("tuzongxun123",
"客戶端授權(quán)碼");
Session session = Session.getInstance(pro, authenticator);
// 根據(jù)Session 構(gòu)建郵件信息
Message message = new MimeMessage(session);
// 創(chuàng)建郵件發(fā)送者地址
Address from = new InternetAddress("tuzongxun123@163.com");
// 設(shè)置郵件消息的發(fā)送者
message.setFrom(from);
// 驗證收件人郵箱地址
List<String> toAddressList = new ArrayList<>();
toAddressList.add("1160569243@qq.com");
StringBuffer buffer = new StringBuffer();
if (!toAddressList.isEmpty()) {
String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(regEx);
for (int i = 0; i < toAddressList.size(); i++) {
Matcher match = p.matcher(toAddressList.get(i));
if (match.matches()) {
buffer.append(toAddressList.get(i));
if (i < toAddressList.size() - 1) {
buffer.append(",");
}
}
}
}
String toAddress = buffer.toString();
if (!toAddress.isEmpty()) {
// 創(chuàng)建郵件的接收者地址
Address[] to = InternetAddress.parse(toAddress);
// 設(shè)置郵件接收人地址
message.setRecipients(Message.RecipientType.TO, to);
// 郵件主題
// message.setSubject("java郵件測試");
message.setSubject("為什么錯了");
// 郵件容器
MimeMultipart mimeMultiPart = new MimeMultipart();
// 設(shè)置HTML
BodyPart bodyPart = new MimeBodyPart();
// 郵件內(nèi)容
// String htmlText = "java郵件測試111";
String htmlText = "為什么錯了";
bodyPart.setContent(htmlText, "text/html;charset=utf-8");
mimeMultiPart.addBodyPart(bodyPart);
// 添加附件
List<String> fileAddressList = new ArrayList<String>();
fileAddressList
.add("C:\\Users\\tuzongxun123\\Desktop\\新建 Microsoft Office Word 文檔.docx");
if (fileAddressList != null) {
BodyPart attchPart = null;
for (int i = 0; i < fileAddressList.size(); i++) {
if (!fileAddressList.get(i).isEmpty()) {
attchPart = new MimeBodyPart();
// 附件數(shù)據(jù)源
DataSource source = new FileDataSource(
fileAddressList.get(i));
// 將附件數(shù)據(jù)源添加到郵件體
attchPart.setDataHandler(new DataHandler(source));
// 設(shè)置附件名稱為原文件名
attchPart.setFileName(MimeUtility.encodeText(source
.getName()));
mimeMultiPart.addBodyPart(attchPart);
}
}
}
message.setContent(mimeMultiPart);
message.setSentDate(new Date());
// 保存郵件
message.saveChanges();
// 發(fā)送郵件
Transport.send(message);
}
}
}
class MailAuthenticator extends Authenticator {
/**
* 用戶名
*/
private String username;
/**
* 密碼
*/
private String password;
/**
* 創(chuàng)建一個新的實例 MailAuthenticator.
*
* @param username
* @param password
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
注:我有個同事使用我這個代碼更換為他的賬號和客戶端授權(quán)碼后,一運(yùn)行就報錯,然后重置了一下郵箱的客戶端授權(quán)碼后,錯誤便消失了。
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)java程序設(shè)計有所幫助。
相關(guān)文章
使用IntelliJ IDEA調(diào)式Stream流的方法步驟
本文主要介紹了使用IntelliJ IDEA調(diào)式Stream流的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
java使用Logback配置輸出日志內(nèi)容到文件示例代碼
這篇文章主要介紹了java?Logback輸出日志內(nèi)容到文件,要將logger.info的信息輸出到文件,您可以使用Logback配置,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
SpringBoot2.x設(shè)置Session失效時間及失效跳轉(zhuǎn)方式
這篇文章主要介紹了SpringBoot2.x設(shè)置Session失效時間及失效跳轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
在SpringBoot項目中實現(xiàn)給所有請求加固定前綴
這篇文章主要介紹了在SpringBoot項目中實現(xiàn)給所有請求加固定前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
ExecutorService實現(xiàn)獲取線程返回值
這篇文章主要介紹了ExecutorService實現(xiàn)獲取線程返回值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08

