![](https://i-blog.csdnimg.cn/blog_migrate/4762a8ec9eb60433747467059e99f95e.gif)
以下三段代码是我的全部代码,朋友们如果想用,直接复制即可。jar包因为我不知道怎么传到javaeye上,所以朋友们回去自己打吧。
我的代码有三个类:
第一个类:MailSenderInfo.java
- packagecom.util.mail;
- /**
- *发送邮件需要使用的基本信息
- */
- importjava.util.Properties;
- publicclassMailSenderInfo{
- //发送邮件的服务器的IP和端口
- privateStringmailServerHost;
- privateStringmailServerPort="25";
- //邮件发送者的地址
- privateStringfromAddress;
- //邮件接收者的地址
- privateStringtoAddress;
- //登陆邮件发送服务器的用户名和密码
- privateStringuserName;
- privateStringpassword;
- //是否需要身份验证
- privatebooleanvalidate=false;
- //邮件主题
- privateStringsubject;
- //邮件的文本内容
- privateStringcontent;
- //邮件附件的文件名
- privateString[]attachFileNames;
- /**
- *获得邮件会话属性
- */
- publicPropertiesgetProperties(){
- Propertiesp=newProperties();
- p.put("mail.smtp.host",this.mailServerHost);
- p.put("mail.smtp.port",this.mailServerPort);
- p.put("mail.smtp.auth",validate?"true":"false");
- returnp;
- }
- publicStringgetMailServerHost(){
- returnmailServerHost;
- }
- publicvoidsetMailServerHost(StringmailServerHost){
- this.mailServerHost=mailServerHost;
- }
- publicStringgetMailServerPort(){
- returnmailServerPort;
- }
- publicvoidsetMailServerPort(StringmailServerPort){
- this.mailServerPort=mailServerPort;
- }
- publicbooleanisValidate(){
- returnvalidate;
- }
- publicvoidsetValidate(booleanvalidate){
- this.validate=validate;
- }
- publicString[]getAttachFileNames(){
- returnattachFileNames;
- }
- publicvoidsetAttachFileNames(String[]fileNames){
- this.attachFileNames=fileNames;
- }
- publicStringgetFromAddress(){
- returnfromAddress;
- }
- publicvoidsetFromAddress(StringfromAddress){
- this.fromAddress=fromAddress;
- }
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- publicStringgetToAddress(){
- returntoAddress;
- }
- publicvoidsetToAddress(StringtoAddress){
- this.toAddress=toAddress;
- }
- publicStringgetUserName(){
- returnuserName;
- }
- publicvoidsetUserName(StringuserName){
- this.userName=userName;
- }
- publicStringgetSubject(){
- returnsubject;
- }
- publicvoidsetSubject(Stringsubject){
- this.subject=subject;
- }
- publicStringgetContent(){
- returncontent;
- }
- publicvoidsetContent(StringtextContent){
- this.content=textContent;
- }
- }
- packagecom.util.mail;
- importjava.util.Date;
- importjava.util.Properties;
- importjavax.mail.Address;
- importjavax.mail.BodyPart;
- importjavax.mail.Message;
- importjavax.mail.MessagingException;
- importjavax.mail.Multipart;
- importjavax.mail.Session;
- importjavax.mail.Transport;
- importjavax.mail.internet.InternetAddress;
- importjavax.mail.internet.MimeBodyPart;
- importjavax.mail.internet.MimeMessage;
- importjavax.mail.internet.MimeMultipart;
- /**
- *简单邮件(不带附件的邮件)发送器
- */
- publicclassSimpleMailSender{
- /**
- *以文本格式发送邮件
- *@parammailInfo待发送的邮件的信息
- */
- publicbooleansendTextMail(MailSenderInfomailInfo){
- //判断是否需要身份认证
- MyAuthenticatorauthenticator=null;
- Propertiespro=mailInfo.getProperties();
- if(mailInfo.isValidate()){
- //如果需要身份认证,则创建一个密码验证器
- authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
- }
- //根据邮件会话属性和密码验证器构造一个发送邮件的session
- SessionsendMailSession=Session.getDefaultInstance(pro,authenticator);
- try{
- //根据session创建一个邮件消息
- MessagemailMessage=newMimeMessage(sendMailSession);
- //创建邮件发送者地址
- Addressfrom=newInternetAddress(mailInfo.getFromAddress());
- //设置邮件消息的发送者
- mailMessage.setFrom(from);
- //创建邮件的接收者地址,并设置到邮件消息中
- Addressto=newInternetAddress(mailInfo.getToAddress());
- mailMessage.setRecipient(Message.RecipientType.TO,to);
- //设置邮件消息的主题
- mailMessage.setSubject(mailInfo.getSubject());
- //设置邮件消息发送的时间
- mailMessage.setSentDate(newDate());
- //设置邮件消息的主要内容
- StringmailContent=mailInfo.getContent();
- mailMessage.setText(mailContent);
- //发送邮件
- Transport.send(mailMessage);
- returntrue;
- }catch(MessagingExceptionex){
- ex.printStackTrace();
- }
- returnfalse;
- }
- /**
- *以HTML格式发送邮件
- *@parammailInfo待发送的邮件信息
- */
- publicstaticbooleansendHtmlMail(MailSenderInfomailInfo){
- //判断是否需要身份认证
- MyAuthenticatorauthenticator=null;
- Propertiespro=mailInfo.getProperties();
- //如果需要身份认证,则创建一个密码验证器
- if(mailInfo.isValidate()){
- authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
- }
- //根据邮件会话属性和密码验证器构造一个发送邮件的session
- SessionsendMailSession=Session.getDefaultInstance(pro,authenticator);
- try{
- //根据session创建一个邮件消息
- MessagemailMessage=newMimeMessage(sendMailSession);
- //创建邮件发送者地址
- Addressfrom=newInternetAddress(mailInfo.getFromAddress());
- //设置邮件消息的发送者
- mailMessage.setFrom(from);
- //创建邮件的接收者地址,并设置到邮件消息中
- Addressto=newInternetAddress(mailInfo.getToAddress());
- //Message.RecipientType.TO属性表示接收者的类型为TO
- mailMessage.setRecipient(Message.RecipientType.TO,to);
- //设置邮件消息的主题
- mailMessage.setSubject(mailInfo.getSubject());
- //设置邮件消息发送的时间
- mailMessage.setSentDate(newDate());
- //MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
- MultipartmainPart=newMimeMultipart();
- //创建一个包含HTML内容的MimeBodyPart
- BodyParthtml=newMimeBodyPart();
- //设置HTML内容
- html.setContent(mailInfo.getContent(),"text/html;charset=utf-8");
- mainPart.addBodyPart(html);
- //将MiniMultipart对象设置为邮件内容
- mailMessage.setContent(mainPart);
- //发送邮件
- Transport.send(mailMessage);
- returntrue;
- }catch(MessagingExceptionex){
- ex.printStackTrace();
- }
- returnfalse;
- }
- }
第三个类:MyAuthenticator.java
- packagecom.util.mail;
- importjavax.mail.*;
- publicclassMyAuthenticatorextendsAuthenticator{
- StringuserName=null;
- Stringpassword=null;
- publicMyAuthenticator(){
- }
- publicMyAuthenticator(Stringusername,Stringpassword){
- this.userName=username;
- this.password=password;
- }
- protectedPasswordAuthenticationgetPasswordAuthentication(){
- returnnewPasswordAuthentication(userName,password);
- }
- }
下面给出使用上面三个类的代码:
- publicstaticvoidmain(String[]args){
- //这个类主要是设置邮件
- MailSenderInfomailInfo=newMailSenderInfo();
- 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("设置邮箱标题");
- mailInfo.setContent("设置邮箱内容");
- //这个类主要来发送邮件
- SimpleMailSendersms=newSimpleMailSender();
- sms.sendTextMail(mailInfo);//发送文体格式
- sms.sendHtmlMail(mailInfo);//发送html格式
- }
最后,给出朋友们几个注意的地方:
1、使用此代码你可以完成你的javamail的邮件发送功能。三个类缺一不可。
2、这三个类我打包是用的com.util.mail包,如果不喜欢,你可以自己改,但三个类文件必须在同一个包中
3、不要使用你刚刚注册过的邮箱在程序中发邮件,如果你的163邮箱是刚注册不久,那你就不要使用“smtp.163.com”。因为你发不出去。刚注册的邮箱是不会给你这种权限的,也就是你不能通过验证。要使用你经常用的邮箱,而且时间比较长的。
4、另一个问题就是mailInfo.setMailServerHost("smtp.163.com");与mailInfo.setFromAddress("han2000lei@163.com");这两句话。即如果你使用163smtp服务器,那么发送邮件地址就必须用163的邮箱,如果不的话,是不会发送成功的。
5、关于javamail验证错误的问题,网上的解释有很多,但我看见的只有一个。就是我的第三个类。你只要复制全了代码,我想是不会有问题的。
-------------------------------------------------------------------------------------------------华丽分割线-----------------------------------------------------------------------------------------------------------------------
老师让做的项目要用到邮箱验证,网上找了半天,这个亲测成功!原帖地址
jar包要在http://www.oracle.com/technetwork/java/javamail/index-138643.html下载!
随便附上常用邮箱:
常用的邮箱服务器(SMTP、POP3)地址、端口
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110) SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110) SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110) SMTP服务器地址:smtp.sohu.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110) SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110) SMTP服务器地址:SMTP.139.com(端口:25)
163.com:
POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995) SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587
HotMail
POP3服务器地址:pop3.live.com (端口:995) SMTP服务器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995) SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
263.net:
POP3服务器地址:pop3.263.net(端口:110) SMTP服务器地址:smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110) SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110) SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110) SMTP服务器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110) SMTP服务器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110) SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110) SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com SMTP服务器地址:smtp.etang.com