使用jsp发送电子邮件时出现错误

rqcrx0a6  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(233)

我有下面的代码发送电子邮件到gmail使用jsp。我已经添加了库,当我运行时,它显示错误,即错误:无法发送邮件。。。。代码有什么问题吗?或者我不得不补充一些东西,因为我不知道还能做什么

<%@page import="java.util.ArrayList"%>
<%@ page import="java.util.,javax.mail."%>
<%@ page import="javax.mail.internet.*" %>

String result;
final String to = "XXX@gmail.com";
final String subject = "subject";
final String messg = "message";

final String from = "xxx@gmail.com";
final String pass = "xxx";

String host = "smtp.gmail.com";

Properties props = new Properties();

props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.user", from);
props.put("mail.password", pass);
props.put("mail.port", "465");

Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(from, pass);
    }
});

try {
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(messg);
    Transport.send(message);
    result = "mail sent successfully";
} catch (MessagingException mex) {
    mex.printStackTrace();
    result = "Error: unable to send mail....";
}
%>

<title>Sending Mail in JSP</title>
<h2><font color="blue">Sending Mail Using JSP</font></h2>
<b><font color="green"><% out.println(result);%></b>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题