java邮件小实例
新建一個包,名為mail
第一個類:MailSenderInfo.java?
###########################################
package com.util.mail; ??
/** ??
* 發(fā)送郵件需要使用的基本信息?
*author by wangfun
http://www.5a520.cn 小說520 ?
*/ ? ?
import java.util.Properties; ? ?
public class MailSenderInfo { ? ?
? ? // 發(fā)送郵件的服務(wù)器的IP和端口 ? ?
? ? private String mailServerHost; ? ?
? ? private String mailServerPort = "25"; ? ?
? ? // 郵件發(fā)送者的地址 ? ?
? ? private String fromAddress; ? ?
? ? // 郵件接收者的地址 ? ?
? ? private String toAddress; ? ?
? ? // 登陸郵件發(fā)送服務(wù)器的用戶名和密碼 ? ?
? ? private String userName; ? ?
? ? private String password; ? ?
? ? // 是否需要身份驗證 ? ?
? ? private boolean validate = false; ? ?
? ? // 郵件主題 ? ?
? ? private String subject; ? ?
? ? // 郵件的文本內(nèi)容 ? ?
? ? private String content; ? ?
? ? // 郵件附件的文件名 ? ?
? ? private String[] attachFileNames; ? ? ?
? ? /** ??
? ? ? * 獲得郵件會話屬性 ??
? ? ? */ ? ?
? ? public Properties getProperties(){ ? ?
? ? ? Properties p = new Properties(); ? ?
? ? ? p.put("mail.smtp.host", this.mailServerHost); ? ?
? ? ? p.put("mail.smtp.port", this.mailServerPort); ? ?
? ? ? p.put("mail.smtp.auth", validate ? "true" : "false"); ? ?
? ? ? return p; ? ?
? ? } ? ?
? ? public String getMailServerHost() { ? ?
? ? ? return mailServerHost; ? ?
? ? } ? ?
? ? public void setMailServerHost(String mailServerHost) { ? ?
? ? ? this.mailServerHost = mailServerHost; ? ?
? ? } ??
? ? public String getMailServerPort() { ? ?
? ? ? return mailServerPort; ? ?
? ? } ??
? ? public void setMailServerPort(String mailServerPort) { ? ?
? ? ? this.mailServerPort = mailServerPort; ? ?
? ? } ??
? ? public boolean isValidate() { ? ?
? ? ? return validate; ? ?
? ? } ??
? ? public void setValidate(boolean validate) { ? ?
? ? ? this.validate = validate; ? ?
? ? } ??
? ? public String[] getAttachFileNames() { ? ?
? ? ? return attachFileNames; ? ?
? ? } ??
? ? public void setAttachFileNames(String[] fileNames) { ? ?
? ? ? this.attachFileNames = fileNames; ? ?
? ? } ??
? ? public String getFromAddress() { ? ?
? ? ? return fromAddress; ? ?
? ? } ? ?
? ? public void setFromAddress(String fromAddress) { ? ?
? ? ? this.fromAddress = fromAddress; ? ?
? ? } ??
? ? public String getPassword() { ? ?
? ? ? return password; ? ?
? ? } ??
? ? public void setPassword(String password) { ? ?
? ? ? this.password = password; ? ?
? ? } ??
? ? public String getToAddress() { ? ?
? ? ? return toAddress; ? ?
? ? } ? ?
? ? public void setToAddress(String toAddress) { ? ?
? ? ? this.toAddress = toAddress; ? ?
? ? } ? ?
? ? public String getUserName() { ? ?
? ? ? return userName; ? ?
? ? } ??
? ? public void setUserName(String userName) { ? ?
? ? ? this.userName = userName; ? ?
? ? } ??
? ? public String getSubject() { ? ?
? ? ? return subject; ? ?
? ? } ??
? ? public void setSubject(String subject) { ? ?
? ? ? this.subject = subject; ? ?
? ? } ??
? ? public String getContent() { ? ?
? ? ? return content; ? ?
? ? } ??
? ? public void setContent(String textContent) { ? ?
? ? ? this.content = textContent; ? ?
? ? } ? ?
} ??
#########################################################
第二個類:SimpleMailSender.java
#############################################################
package com.util.mail; ??
??
import java.util.Date; ? ?
import java.util.Properties; ??
import javax.mail.Address; ? ?
import javax.mail.BodyPart; ? ?
import javax.mail.Message; ? ?
import javax.mail.MessagingException; ? ?
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; ? ?
??
/** ??
* 簡單郵件(不帶附件的郵件)發(fā)送器 ??
http://www.bt285.cn BT下載
*/ ? ?
public class SimpleMailSender ?{ ? ?
/** ??
? * 以文本格式發(fā)送郵件 ??
? * @param mailInfo 待發(fā)送的郵件的信息 ??
? */ ? ?
? ? public boolean sendTextMail(MailSenderInfo mailInfo) { ? ?
? ? ? // 判斷是否需要身份認證 ? ?
? ? ? MyAuthenticator authenticator = null; ? ?
? ? ? Properties pro = mailInfo.getProperties(); ??
? ? ? if (mailInfo.isValidate()) { ? ?
? ? ? // 如果需要身份認證,則創(chuàng)建一個密碼驗證器 ? ?
? ? ? ? authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); ? ?
? ? ? } ??
? ? ? // 根據(jù)郵件會話屬性和密碼驗證器構(gòu)造一個發(fā)送郵件的session ? ?
? ? ? Session sendMailSession = Session.getDefaultInstance(pro,authenticator); ? ?
? ? ? try { ? ?
? ? ? // 根據(jù)session創(chuàng)建一個郵件消息 ? ?
? ? ? Message mailMessage = new MimeMessage(sendMailSession); ? ?
? ? ? // 創(chuàng)建郵件發(fā)送者地址 ? ?
? ? ? Address from = new InternetAddress(mailInfo.getFromAddress()); ? ?
? ? ? // 設(shè)置郵件消息的發(fā)送者 ? ?
? ? ? mailMessage.setFrom(from); ? ?
? ? ? // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中 ? ?
? ? ? Address to = new InternetAddress(mailInfo.getToAddress()); ? ?
? ? ? mailMessage.setRecipient(Message.RecipientType.TO,to); ? ?
? ? ? // 設(shè)置郵件消息的主題 ? ?
? ? ? mailMessage.setSubject(mailInfo.getSubject()); ? ?
? ? ? // 設(shè)置郵件消息發(fā)送的時間 ? ?
? ? ? mailMessage.setSentDate(new Date()); ? ?
? ? ? // 設(shè)置郵件消息的主要內(nèi)容 ? ?
? ? ? String mailContent = mailInfo.getContent(); ? ?
? ? ? mailMessage.setText(mailContent); ? ?
? ? ? // 發(fā)送郵件 ? ?
? ? ? Transport.send(mailMessage); ??
? ? ? return true; ? ?
? ? ? } catch (MessagingException ex) { ? ?
? ? ? ? ? ex.printStackTrace(); ? ?
? ? ? } ? ?
? ? ? return false; ? ?
? ? } ? ?
? ? ? ?
? ? /** ??
? ? ? * 以HTML格式發(fā)送郵件 ??
? ? ? * @param mailInfo 待發(fā)送的郵件信息 ??
? ? ? */ ? ?
? ? public static boolean sendHtmlMail(MailSenderInfo mailInfo){ ? ?
? ? ? // 判斷是否需要身份認證 ? ?
? ? ? MyAuthenticator authenticator = null; ??
? ? ? Properties pro = mailInfo.getProperties(); ??
? ? ? //如果需要身份認證,則創(chuàng)建一個密碼驗證器 ? ??
? ? ? if (mailInfo.isValidate()) { ? ?
? ? ? ? authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); ??
? ? ? } ? ?
? ? ? // 根據(jù)郵件會話屬性和密碼驗證器構(gòu)造一個發(fā)送郵件的session ? ?
? ? ? Session sendMailSession = Session.getDefaultInstance(pro,authenticator); ? ?
? ? ? try { ? ?
? ? ? // 根據(jù)session創(chuàng)建一個郵件消息 ? ?
? ? ? Message mailMessage = new MimeMessage(sendMailSession); ? ?
? ? ? // 創(chuàng)建郵件發(fā)送者地址 ? ?
? ? ? Address from = new InternetAddress(mailInfo.getFromAddress()); ? ?
? ? ? // 設(shè)置郵件消息的發(fā)送者 ? ?
? ? ? mailMessage.setFrom(from); ? ?
? ? ? // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中 ? ?
? ? ? Address to = new InternetAddress(mailInfo.getToAddress()); ? ?
? ? ? // Message.RecipientType.TO屬性表示接收者的類型為TO ? ?
? ? ? mailMessage.setRecipient(Message.RecipientType.TO,to); ? ?
? ? ? // 設(shè)置郵件消息的主題 ? ?
? ? ? mailMessage.setSubject(mailInfo.getSubject()); ? ?
? ? ? // 設(shè)置郵件消息發(fā)送的時間 ? ?
? ? ? mailMessage.setSentDate(new Date()); ? ?
? ? ? // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 ? ?
? ? ? Multipart mainPart = new MimeMultipart(); ? ?
? ? ? // 創(chuàng)建一個包含HTML內(nèi)容的MimeBodyPart ? ?
? ? ? BodyPart html = new MimeBodyPart(); ? ?
? ? ? // 設(shè)置HTML內(nèi)容 ? ?
? ? ? html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); ? ?
? ? ? mainPart.addBodyPart(html); ? ?
? ? ? // 將MiniMultipart對象設(shè)置為郵件內(nèi)容 ? ?
? ? ? mailMessage.setContent(mainPart); ? ?
? ? ? // 發(fā)送郵件 ? ?
? ? ? Transport.send(mailMessage); ? ?
? ? ? return true; ? ?
? ? ? } catch (MessagingException ex) { ? ?
? ? ? ? ? ex.printStackTrace(); ? ?
? ? ? } ? ?
? ? ? return false; ? ?
? ? } ? ?
} ??
#######################################################
第三個類:MyAuthenticator.java?
########################################################
package com.util.mail; ??
??
import javax.mail.*; ??
? ? ?
public class MyAuthenticator extends Authenticator{ ??
? ? String userName=null; ??
? ? String password=null; ??
? ? ? ??
? ? public MyAuthenticator(){ ??
? ? } ??
? ? public MyAuthenticator(String username, String password) { ? ?
? ? ? ? this.userName = username; ? ?
? ? ? ? this.password = password; ? ?
? ? } ? ?
? ? protected PasswordAuthentication getPasswordAuthentication(){ ??
? ? ? ? return new PasswordAuthentication(userName, password); ??
? ? } ??
} ??
############################################################
下面給出使用上面三個類的代碼:
############################################################
public static void main(String[] args){ ??
? ? ? ? ?//這個類主要是設(shè)置郵件 ??
? ? ? MailSenderInfo mailInfo = new MailSenderInfo(); ? ?
? ? ? mailInfo.setMailServerHost("smtp.163.com"); ? ?
? ? ? mailInfo.setMailServerPort("25"); ? ?
? ? ? mailInfo.setValidate(true); ? ?
? ? ? mailInfo.setUserName("han2000lei@163.com"); ? ?
? ? ? mailInfo.setPassword("**********");//您的郵箱密碼 ? ?
? ? ? mailInfo.setFromAddress("han2000lei@163.com"); ? ?
? ? ? mailInfo.setToAddress("han2000lei@163.com"); ? ?
? ? ? mailInfo.setSubject("設(shè)置郵箱標(biāo)題 如http://www.guihua.org 中國桂花網(wǎng)"); ? ?
? ? ? mailInfo.setContent("設(shè)置郵箱內(nèi)容 如http://www.guihua.org 中國桂花網(wǎng) 是中國最大桂花網(wǎng)站=="); ? ?
? ? ? ? ?//這個類主要來發(fā)送郵件 ??
? ? ? SimpleMailSender sms = new SimpleMailSender(); ??
? ? ? ? ? sms.sendTextMail(mailInfo);//發(fā)送文體格式 ? ?
? ? ? ? ? sms.sendHtmlMail(mailInfo);//發(fā)送html格式 ??
? ? } ?
##################################################################
最后,給出朋友們幾個注意的地方:?
1、使用此代碼你可以完成你的javamail的郵件發(fā)送功能。三個類缺一不可。?
2、這三個類我打包是用的com.util.mail包,如果不喜歡,你可以自己改,但三個類文件必須在同一個包中?
3、不要使用你剛剛注冊過的郵箱在程序中發(fā)郵件,如果你的163郵箱是剛注冊不久,那你就不要使用“smtp.163.com”。因為你發(fā)不出去。剛注冊的郵箱是不會給你這種權(quán)限的,也就是你不能通過驗證。要使用你經(jīng)常用的郵箱,而且時間比較長的。?
4、另一個問題就是mailInfo.setMailServerHost("smtp.163.com");與mailInfo.setFromAddress("han2000lei@163.com");這兩句話。即如果你使用163smtp服務(wù)器,那么發(fā)送郵件地址就必須用163的郵箱,如果不的話,是不會發(fā)送成功的。?
5、關(guān)于javamail驗證錯誤的問題,網(wǎng)上的解釋有很多,但我看見的只有一個。就是我的第三個類。你只要復(fù)制全了代碼,我想是不會有問題的。
Eclipse下如何導(dǎo)入jar包:http://blog.csdn.net/justinavril/article/details/2783182
1.右擊工程的根目錄,點擊屬性進入屬性。
2.在屬性頁面中選中Java Build Path,選中 庫 標(biāo)簽,點擊Add External JARs。
3.找到需要添加的jar包,確定即可。
總結(jié)
- 上一篇: 迷宫问题算法分析
- 下一篇: 浏览器兼容性 问题收集