我在servlet中写的邮件发送,老是提示javax.mail.AuthenticationFailedException错误,查了半天也没查出来是什么原因,请各位高手帮帮忙,源码如下:
String smtpServer = request.getParameter( "txtserver ");
String emailTo = request.getParameter( "txtto ");
String fromemail = request.getParameter( "txtfrom ");
String subject = request.getParameter( "txtsubject ");
String body = request.getParameter( "txtmessage ");
try
{
Properties props = new Properties() ;
props.put( "mail.transport.protocol ", "smtp ");
props.put( "mail.smtp.auth ", "true ");
props.put( "mail.smtp.host ",smtpServer);
props.put( "mail.smtp.port ", "25 ");
Session mailsession = Session.getInstance(props);
Message msg = new MimeMessage(mailsession);
msg.setFrom(new InternetAddress(fromemail));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(emailTo));
msg.setSentDate(new Date());
msg.setSubject(subject);
msg.setText(body);
msg.saveChanges() ;
// Transport transport;
// transport = mailsession.getTransport( "smtp ");
// transport.connect((String)props.get( "smtp.163.com "), "caojunkeke ", "06240624 ");
Transport.send(msg);
msg.writeTo(System.out );
out.print( "邮件已成功发送到 ");
}
catch(Exception e)
{
System.out.println(e);
}
------解决方法--------------------------------------------------------
需要验证,当然不行了
final String username = "username ";
final String password = "password ";
//使用验证
Session session = Session.getDefaultInstance(props,