throws AddressException, MessagingException {
// 1.創(chuàng)建一個(gè)程序與郵件服務(wù)器會(huì)話對(duì)象 Session
Properties props = new Properties();
props.setProperty(“mail.transport.protocol”, “SMTP”);
props.setProperty(“mail.host”, “smtp.163.com”);
props.setProperty(“mail.smtp.auth”, “true”);// 指定驗(yàn)證為true
// 創(chuàng)建驗(yàn)證器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“cui******@163.com”, “12313456”);//注冊(cè)郵箱的帳號(hào)和授權(quán)碼
}
};
Session session = Session.getInstance(props, auth);
// 2.創(chuàng)建一個(gè)Message,它相當(dāng)于是郵件內(nèi)容
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(“**********@163.com”,”8896″));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 設(shè)置發(fā)送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設(shè)置發(fā)送方式與接收者
message.setSubject(“用戶激活”);
// message.setText(“這是一封激活郵件,請(qǐng)<a href=\\\’#\\\’>點(diǎn)擊</a>”);
message.setContent(emailMsg, “text/html;charset=utf-8″);
// 3.創(chuàng)建 Transport用于將郵件發(fā)送
Transport.send(message);
}
}
這里我用的是網(wǎng)易郵箱,郵箱需要設(shè)置開(kāi)通smtp協(xié)議,得到授權(quán)碼
測(cè)試類
public class Test { public static void main(String[] args) { try {
String emailMsg=”這是一封激活郵件”; MailUtils.sendMail(驗(yàn)證的郵箱, emailMsg); System.out.println(“郵件發(fā)送成功!”); } catch (MessagingException e) { e.printStackTrace(); } } }
如果,你對(duì)上面的內(nèi)容還有疑問(wèn),推薦選擇西部數(shù)碼企業(yè)云郵箱!有專人協(xié)助您解答郵箱疑問(wèn)。
西部數(shù)碼是專業(yè)企業(yè)郵箱的官方正規(guī)提供商,21年行業(yè)經(jīng)驗(yàn),提供安全穩(wěn)定,簡(jiǎn)單易用,高性價(jià)比的企業(yè)郵箱,按需自由定制,不限空間,極速收發(fā),能夠滿足用戶對(duì)企業(yè)郵箱的不同需求??梢酝ㄟ^(guò)以下幾種方式注冊(cè)、申請(qǐng)、購(gòu)買、試用、開(kāi)通企業(yè)郵箱:
1、登錄http://youyuetrip.com/services/mail/在線咨詢申請(qǐng)?jiān)囉没蛸?gòu)買;
2、直接致電028-62778877申請(qǐng)?jiān)囉没蛘劫?gòu)買開(kāi)通;