Javamail配置阿里云邮箱发送邮件

阅读:923 2019-03-20 14:08:41 来源:新网

先了解一下基本的配置元素的概念:

什么是pop3、smtp?

1、什么是pop3:

pop3是postofficeprotocol3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,pop3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而pop3服务器则是遵循pop3协议的接收邮件服务器,用来接收电子邮件的。

2、什么是smtp:

smtp的全称是“simplemailtransferprotocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。smtp协议属于tcp/ip协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。smtp服务器就是遵循smtp协议的发送邮件服务器。

(smtp认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录smtp服务器,这就使得那些垃圾邮件的散播者无可乘之机。增加smtp认证的目的是为了使用户避免受到垃圾邮件的侵扰。)

1,引入的包:

javax.mailmail1.4.7

2,邮箱信息在properties中的相关配置:

smtpserver=smtp.aliyun.comport=465fromusername=你的阿里邮箱账号fromuserpassword=你的邮箱密码

这里注意了,很容易出错,网上一搜基本的教程都是这样的:smtpserver=smtp.aliyun.com

也就是properties.put("mail.smtp.host",smtp.aliyun.com);

但如果你使用的是阿里的企业邮箱,这样把邮箱的配置服务器地址照搬过来的做法是有问题的。

这里应该使用自己企业的域名地址,比如我的域名是amuxia.com(当然这个也是假的,举个例子,哈哈),这里就应该配置smtp.amuxia.com。否则报错:

这里应该注意一下。

3,邮箱实体类(设置邮箱、邮件的相关信息)

publicclassemailinfo{privatefinalstringssl_factory="javax.net.ssl.sslsocketfactory";privatestringsmtpserver;//smtp服务器地址privatestringport;//端口privatestringfromusername;//登录smtp服务器的用户名,发送人邮箱地址privatestringfromuserpassword;//登录smtp服务器的密码privatestringtouser;//收件人privatestringsubject;//邮件主题privatestringcontent;//邮件正文publicemailinfo(){}publicemailinfo(stringtouser,stringsubject,stringcontent){this.touser=touser;this.subject=subject;this.content=content;this.smtpserver=global.getconfig("smtpserver");this.port=global.getconfig("port");this.fromusername=global.getconfig("fromusername");this.fromuserpassword=global.getconfig("fromuserpassword");}//get、set方法略}

4,发送邮件的实现类(工具类):

publicclassemailutil{/***进行base64加密,防止中文乱码*/privatestaticstringchangeencode(stringstr){try{str=mimeutility.encodetext(newstring(str.getbytes(),"utf-8"),"utf-8","b");//"b"代表base64}catch(unsupportedencodingexceptione){e.printstacktrace();}returnstr;}publicstaticbooleansendhtmlmail(emailinfoemailinfo){propertiesproperties=newproperties();properties.put("mail.smtp.host",emailinfo.getsmtpserver());properties.put("mail.transport.protocol","smtp");properties.put("mail.smtp.auth","true");properties.put("mail.smtp.socketfactory.class","javax.net.ssl.sslsocketfactory");//使用jsse的sslproperties.put("mail.smtp.socketfactory.fallback","false");//只处理ssl的连接,对于非ssl的连接不做处理properties.put("mail.smtp.port",emailinfo.getport());properties.put("mail.smtp.socketfactory.port",emailinfo.getport());sessionsession=session.getinstance(properties);session.setdebug(true);mimemessagemessage=newmimemessage(session);try{//发件人addressaddress=newinternetaddress(emailinfo.getfromusername());message.setfrom(address);//收件人addresstoaddress=newinternetaddress(emailinfo.gettouser());message.setrecipient(mimemessage.recipienttype.to,toaddress);//设置收件人,并设置其接收类型为to//主题message.setsubject(changeencode(emailinfo.getsubject()));message.setsubject(emailinfo.getsubject());//时间message.setsentdate(newdate());multipartmultipart=newmimemultipart();//创建一个包含html内容的mimebodypartbodyparthtml=newmimebodypart();//设置html内容html.setcontent(emailinfo.getcontent(),"text/html;charset=utf-8");multipart.addbodypart(html);//将minimultipart对象设置为邮件内容message.setcontent(multipart);message.savechanges();}catch(exceptione){e.printstacktrace();returnfalse;}try{transporttransport=session.gettransport("smtp");transport.connect(emailinfo.getsmtpserver(),emailinfo.getfromusername(),emailinfo.getfromuserpassword());transport.sendmessage(message,message.getallrecipients());transport.close();}catch(exceptione){e.printstacktrace();returnfalse;}returntrue;}}5,测试一下:

publicstaticvoidmain(string[]args){emailutilutil=newemailutil();stringcontent=global.getconfig("email_user_add_content");content=content.format(content,"111","222");system.out.println(content);emailinfoinfo=newemailinfo("amuxia@163.com",global.getconfig("email_user_add_subject"),"

这是一个测试邮件

");util.sendhtmlmail(info);}这下就妥妥的了,这些代码运行没有问题,但是用到了其他的一些辅助类,如global.getconfig()获取配置文件中的信息,用时替换掉就行。

应用中配置使用邮箱接收发送邮件,经常会因为各个邮箱配置的细微差异出现错误,多半是认证不通过,而认证不通过的原因无非是:

1、服务器错误

2、用户名错误

3、用户名密码不匹配。

遇到错误从这几方面下手就可以了,在代码中使用邮箱发送邮件时要先在客户端试一次,确保邮箱在客户端是可接可收的。

1,点击文件——》添加账户

2,选择配置方式

3,填写账户信息:

4,点击“其他设置”

最后就完成了配置,发送一封邮件测试一下就妥妥的了。

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服