java邮件发送功能_Java实现邮件发送功能

importjava.util.Properties;

importjavax.mail.Address;

importjavax.mail.MessagingException;

importjavax.mail.Session;

importjavax.mail.Transport;

importjavax.mail.internet.InternetAddress;

importjavax.mail.internet.MimeMessage;

publicclassSendEmailUtil{

publicstaticbooleansendMail(StringemailAddress,StringemailMsg){

//谁发送(发送方)

Stringfrom=””;

//发给谁

Stringto=emailAddress;

//发送者的用户名和密码(邮箱登录用)

finalStringusername=””;//此处填写发送的邮箱名

finalStringpassword=””;//此处填写登录的邮箱密码

//定义properties对象,设置环境信息85734a13d9f53834152a628e08395f5e.png

Propertiesproperties=newProperties();

/*

*mail.smtp.host:指定衔接的邮件效劳器的主机名。如:163邮箱就填写smtp.163.com

*若在本地测试的话,需求在本地装置smtp效劳器

*/

properties.setProperty(“mail.smtp.host”,”smtp.163.com”);

//mail.smtp.auth:指定客户端能否要向邮件效劳器提交考证

properties.setProperty(“mail.smtp.auth”,”true”);

/*

*mail.transport.protocol:指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件

*mail.store.protocol:指定邮件接纳协议

*/

properties.setProperty(“mail.transport.protocol”,”smtp”);

//获取session对象

Sessionsession=Session.getInstance(properties);

//当设置为true,JavaMailAP就会将其运转过程和邮件效劳器的交互命令信息输出到console中,用于JavaMail的调试

session.setDebug(true);

try{

//创立邮件对象

MimeMessagemessage=newMimeMessage(session);

//设置邮件发送方

message.setFrom(newInternetAddress(from));

//设置邮件发送的主题

message.setSubject(“邮件发送设置”);

//设置邮件发送的内容

message.setContent(emailMsg,”text/html;charset=utf-8″);

Transporttransport=session.getTransport();

//衔接邮件效劳器,“”中填写邮件效劳器主机名

transport.connect(“”,25,username,password);

transport.sendMessage(message,newAddress[]{newInternetAddress(to)});

transport.close();

returntrue;

}catch(MessagingExceptione){

e.printStackTrace();

returnfalse;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值