用JAVA MAIL实现发送邮件的功能

项目需求说明 在WEB项目开发中 我们往往需要实现向用户发送邮件的功能,利用JAVA的JAVAMAIL开发包 我们可以方便的实现发送邮件的功能。

首先我们必须先下载javamail.jar和JAF框架(处理多种格式)集成到ECLIPSE中

下面代码实现了 向用户发送邮件的功能 ,在开发中使用了公司免费邮箱的邮件服务器(需要身份认证)

 

  String email=setPasswordByEmailForm.getEmail();
  try{
   Properties props = new Properties();
   Session sendMailSession;
   Store store;
   Transport transport;
   props.put("mail.smtp.host", "mail.BIANCHENG.cn"); //设置邮件服务器
   props.put("mail.smtp.auth","true");
   
   Authenticator auto=new  Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication("service@BIANCHENG.cn", "123456");//身份认证
            }
          };
       

   sendMailSession = Session.getDefaultInstance(props, auto);
   
   Message newMessage = new MimeMessage(sendMailSession);
   newMessage.setFrom(new InternetAddress("service@BIANCHENG.cn"));
   newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
   newMessage.setSubject("请修改您的密码");
   newMessage.setSentDate(new Date());
   newMessage.setText("请尽快登陆http://www.BIANCHENG。CN/byEmail.jsp修改你的密码");
   transport = sendMailSession.getTransport("smtp");
   transport.send(newMessage);
  
   }
   catch(MessagingException m)
   {
    System.out.println("");
   }

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值