项目需求说明 在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("");
}