import java.util.Date;
定海網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)建站2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 javax.mail.internet.MimeUtility;
/**
?* javaMail發(fā)送郵件工具類
?*?
?* @author
?* @version 1.0
?*/
public class MailUtil {
? ? /**
? ? ?* 附件地址
? ? ?*/
? ? private String filepath;
? ? /**
? ? ?* 取附件
? ? ?*?
? ? ?* @return filepath
? ? ?*/
? ? public String getFilepath() {
? ? ? ? return filepath;
? ? }
? ? /**
? ? ?* 設(shè)置附件
? ? ?*?
? ? ?* @param filepath filepath
? ? ?*/
? ? public void setFilepath(String filepath) {
? ? ? ? this.filepath = filepath;
? ? }
? ? /**
? ? ?* 發(fā)送郵件的服務(wù)器地址
? ? ?*/
? ? private String mailServerHost = "smtp.exmail.qq.com";
? ? /**
? ? ?* 發(fā)送郵件的服務(wù)器端口
? ? ?*/
? ? private String mailServerPort = "25";
? ? /**
? ? ?* 設(shè)置發(fā)送郵件前是否需要驗證
? ? ?*/
? ? private boolean validate = true;
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的賬號
? ? ?*/
? ? private String userName = "235583@qq.com";
? ? /**
? ? ?* 郵件密碼
? ? ?*/
? ? private String userPass = "Cd1555";
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的地址
? ? ?*/
? ? private String fromAddress = "23558@qq.com";
? ? /**
? ? ?* 郵件發(fā)送對象
? ? ?*/
? ? private String toAddress = "2355836@qq.com";
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的主題
? ? ?*/
? ? private String subject = "測試java發(fā)送郵件";
? ? /**
? ? ?* 郵件內(nèi)容
? ? ?*/
? ? private String content = "您好!<br/>這里用java發(fā)送郵件的測試內(nèi)容<br/><br/>-<br/>";
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的格式是否采用html編碼
? ? ?*/
? ? private boolean isHTML = true;
? ? /**
? ? ?* 設(shè)置發(fā)送郵件是否采用安全連接
? ? ?*/
? ? private boolean isSSL = false;
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的主機
? ? ?*?
? ? ?* @param mailServerHost
? ? ?*? ? ? ? ? ? 發(fā)送郵件的服務(wù)器地址
? ? ?*/
? ? public void setMailServerHost(String mailServerHost) {
? ? ? ? this.mailServerHost = mailServerHost;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的服務(wù)器端口 <br/>
? ? ?* 說明:默認(rèn)值"25"
? ? ?*?
? ? ?* @param mailServerPort
? ? ?*? ? ? ? ? ? 發(fā)送郵件的服務(wù)器端口
? ? ?*/
? ? public void setMailServerPort(String mailServerPort) {
? ? ? ? this.mailServerPort = mailServerPort;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件前是否需要驗證 <br/>
? ? ?* 說明:默認(rèn)值true要驗證
? ? ?*?
? ? ?* @param validate
? ? ?*? ? ? ? ? ? true要驗證 or false不驗證
? ? ?*/
? ? public void setValidate(boolean validate) {
? ? ? ? this.validate = validate;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的地址 <br/>
? ? ?* 說明:默認(rèn)值"xxxxxxx@qq.com"
? ? ?*?
? ? ?* @param fromAddress
? ? ?*? ? ? ? ? ? 發(fā)送郵件的地址
? ? ?*/
? ? public void setFromAddress(String fromAddress) {
? ? ? ? this.fromAddress = fromAddress;
? ? }
? ? /**
? ? ?* 設(shè)置接收郵件的地址 <br/>
? ? ?* 說明:默認(rèn)值"xxxxxxxx@qq.com"
? ? ?*?
? ? ?* @param toAddress
? ? ?*? ? ? ? ? ? 接收郵件的地址
? ? ?*/
? ? public void setToAddress(String toAddress) {
? ? ? ? this.toAddress = toAddress;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的主題 <br/>
? ? ?* 說明:默認(rèn)值"測試java發(fā)送郵件"
? ? ?*?
? ? ?* @param subject
? ? ?*? ? ? ? ? ? 發(fā)送郵件的主題
? ? ?*/
? ? public void setSubject(String subject) {
? ? ? ? this.subject = subject;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的內(nèi)容 <br/>
? ? ?* 說明:默認(rèn)值"您好!<br/>
? ? ?* 這里用java發(fā)送郵件的測試內(nèi)容<br/>
? ? ?* <br/>
? ? ?* -頂呱呱<br/>
? ? ?* "
? ? ?*?
? ? ?* @param content
? ? ?*? ? ? ? ? ? 發(fā)送郵件的內(nèi)容
? ? ?*/
? ? public void setContent(String content) {
? ? ? ? this.content = content;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的格式是否采用html編碼 <br/>
? ? ?* 說明:默認(rèn)true是
? ? ?*?
? ? ?* @param isHTML
? ? ?*? ? ? ? ? ? true是 false否
? ? ?*/
? ? public void setHTML(boolean isHTML) {
? ? ? ? this.isHTML = isHTML;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件是否采用安全連接 <br/>
? ? ?* 說明:默認(rèn)false否
? ? ?*?
? ? ?* @param isSSL
? ? ?*? ? ? ? ? ? true是 false否
? ? ?*/
? ? public void setSSL(boolean isSSL) {
? ? ? ? this.isSSL = isSSL;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的賬號
? ? ?*?
? ? ?* @param userName
? ? ?*? ? ? ? ? ? 發(fā)送郵件的賬號
? ? ?*/
? ? public void setUserName(String userName) {
? ? ? ? this.userName = userName;
? ? }
? ? /**
? ? ?* 設(shè)置發(fā)送郵件的賬號密碼
? ? ?*?
? ? ?* @param userPass
? ? ?*? ? ? ? ? ? 發(fā)送郵件的賬號密碼
? ? ?*/
? ? public void setUserPass(String userPass) {
? ? ? ? this.userPass = userPass;
? ? }
? ? /**
? ? ?* 發(fā)送郵件
? ? ?*?
? ? ?* @param toAddress
? ? ?*? ? ? ? ? ? 收件人郵箱地址
? ? ?* @param subject
? ? ?*? ? ? ? ? ? 郵件主題
? ? ?* @param content
? ? ?*? ? ? ? ? ? 郵件內(nèi)容
? ? ?* @param filepath
? ? ?*? ? ? ? ? ? 附件地址
? ? ?* @return true:發(fā)送成功;false:發(fā)送失敗
? ? ?*/
? ? public boolean sendMail(String toAddress, String subject, String content,
? ? ? ? ? ? String filepath) {
? ? ? ? this.toAddress = toAddress;
? ? ? ? this.subject = subject;
? ? ? ? this.content = content;
? ? ? ? this.filepath = filepath;
? ? ? ? return sendMail();
? ? }
? ? /**
? ? ?* 發(fā)送郵件
? ? ?*?
? ? ?* @return true:發(fā)送成功;false:發(fā)送失敗
? ? ?*/
? ? public boolean sendMail() {
? ? ? ? Properties p = new Properties();
? ? ? ? p.put("mail.smtp.host", mailServerHost);
? ? ? ? p.put("mail.smtp.port", mailServerPort);
? ? ? ? p.put("mail.smtp.auth", validate ? "true" : "false");
? ? ? ? if (isSSL) {
? ? ? ? ? ? p.put("mail.smtp.starttls.enable", "true");
? ? ? ? ? ? p.put("mail.smtp.socketFactory.fallback", "false");
? ? ? ? ? ? p.put("mail.smtp.socketFactory.port", mailServerPort);
? ? ? ? }
? ? ? ? Authenticator auth = null;
? ? ? ? if (validate) {
? ? ? ? ? ? auth = new MyAuthenticator(userName, userPass);
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? Session session = Session.getDefaultInstance(p, auth);
? ? ? ? ? ? Message message = new MimeMessage(session);
? ? ? ? ? ? Address from = new InternetAddress(fromAddress);
? ? ? ? ? ? Address to = new InternetAddress(toAddress);
? ? ? ? ? ? message.setFrom(from);
? ? ? ? ? ? message.setRecipient(Message.RecipientType.TO, to);
? ? ? ? ? ? message.setSubject(subject);
? ? ? ? ? ? message.setSentDate(new Date());
? ? ? ? ? ? if (isHTML) {
? ? ? ? ? ? ? ? Multipart m = new MimeMultipart();
? ? ? ? ? ? ? ? BodyPart bp = new MimeBodyPart();
? ? ? ? ? ? ? ? bp.setContent(content, "text/html; charset=utf-8");
? ? ? ? ? ? ? ? // 加入附件
? ? ? ? ? ? ? ? FileDataSource ds = new FileDataSource(filepath);
? ? ? ? ? ? ? ? bp.setDataHandler(new DataHandler(ds));
? ? ? ? ? ? ? ? bp.setFileName(MimeUtility.encodeText(ds.getName()));
? ? ? ? ? ? ? ? m.addBodyPart(bp);
? ? ? ? ? ? ? ? message.setContent(m);
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? message.setText(content);
? ? ? ? ? ? Transport.send(message);
? ? ? ? ? ? return true;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
? ? /**
? ? ?* 測試發(fā)送
? ? ?*?
? ? ?* @param args
? ? ?*? ? ? ? ? ? 參數(shù)
? ? ?*/
? ? public static void main(String[] args) {
? ? ? ? boolean x = new MailUtil().sendMail();
? ? ? ? LoggerUtils.info(x);
? ? }
}
文章題目:利用javaMail發(fā)送郵件的方法
URL網(wǎng)址:http://muchs.cn/article18/ijsogp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、App開發(fā)、電子商務(wù)、微信公眾號、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)