使用java发送邮箱

阅读:1058 2019-03-20 14:08:24 来源:新网

使用java发送邮箱以qq邮箱为例说明发送邮箱

com.sun.mailjavax.mail1.6.0qq邮箱开启pop3/smtp服务

设置-.>账户->pop3/imap/smtp/exchange/carddav/caldav服务开启pop3/smtp服务生成授权码后将授权码保存,后面会用

packagecom.spring.until;importjava.security.generalsecurityexception;importjava.util.properties;importjavax.mail.authenticator;importjavax.mail.message;importjavax.mail.messagingexception;importjavax.mail.passwordauthentication;importjavax.mail.session;importjavax.mail.transport;importjavax.mail.internet.internetaddress;importjavax.mail.internet.mimemessage;importcom.sun.mail.util.mailsslsocketfactory;publicclasssendemail{publicstaticbooleansend(stringemail,stringtitle,stringbody)throwsgeneralsecurityexception{//收件人电子邮箱stringto=email;//发件人电子邮箱stringfrom=propertiesuntil.properties("account");//指定发送邮件的主机为smtp.qq.comstringhost="smtp.qq.com";//qq邮件服务器//获取系统属性propertiesproperties=system.getproperties();//设置邮件服务器properties.setproperty("mail.smtp.host",host);properties.put("mail.smtp.auth","true");mailsslsocketfactorysf=newmailsslsocketfactory();sf.settrustallhosts(true);properties.put("mail.smtp.ssl.enable","true");properties.put("mail.smtp.ssl.socketfactory",sf);//获取默认session对象sessionsession=session.getdefaultinstance(properties,newauthenticator(){publicpasswordauthenticationgetpasswordauthentication(){returnnewpasswordauthentication(propertiesuntil.properties("account"),propertiesuntil.properties("password"));//发件人邮件用户名、密码}});try{//创建默认的mimemessage对象mimemessagemessage=newmimemessage(session);//setfrom:头部头字段message.setfrom(newinternetaddress(from));//setto:头部头字段message.addrecipient(message.recipienttype.to,newinternetaddress(to));//setsubject:头部头字段message.setsubject(title);//设置消息体message.settext(body);//发送消息transport.send(message);returntrue;}catch(messagingexceptionmex){returnfalse;}}}

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

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服