我写了一个简单的程序来发送电子邮件。 servlet程序和JSP代码如下。我在jsp页面中收到以下错误消息。我正在使用glassfish服务器。我已将以下jar文件添加到我的类路径中。
我在Windows 7平台上工作并使用netbeans IDE。
发件人电子邮件地址= someone@mohp.gov.np 收件人电子邮件id = someone@gmail.com
Couldn't connect to host, port: localhost, 25; timeout -1
String err="";
//reciever email ID
//reciever email id=someone@gmail.com
String to=request.getParameter("reciever");
//sender emai ID
//sender email id=someone@mohp.gov.np
String from=request.getParameter("sender");
//Assuming sending email from localhost
String host="localhost";
//Subject of the email;
String sub=request.getParameter("subject");
//message of the email
String msg=request.getParameter("message");
//get system properties
Properties properties=System.getProperties();
//Setup mail server
properties.setProperty("mail.smtp.host", host);
//get the default Session object
Session session=Session.getDefaultInstance(properties);
try{
//Create a default MimeMessage object.
MimeMessage message=new MimeMessage(session);
//set FROM: header field of the header.
message.setFrom(new InternetAddress(from));
//set TO: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//set subject:
message.setSubject(sub);
//set message:
message.setText(msg);
//Send message
Transport.send(message);
}catch (MessagingException mex) {
mex.printStackTrace();
err=mex.getMessage();
}
request.setAttribute("err", err);
RequestDispatcher rd=request.getRequestDispatcher("/newjsp.jsp");
rd.forward(request, response);
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/email.css" type="text/css">
<title>E-Mail</title>
</head>
<body>
<form action="EMail" method="post" name="myform">
<table border="1" width="100%" height="600px" cell-padding="0">
<tr>
<td><table>
<tr height="30px">
<td width="5%" align="left">From :</td>
<td width="80%"><input type="text" name="sender" value="" size="80" /></td>
</tr>
<tr height="30px">
<td width="5%">To :</td>
<td width="80%"><input type="text" name="reciever" value="" size="80" /></td>
</tr>
<tr height="30px">
<td width="5%">Subject :</td>
<td width="80%"><input type="text" name="subject" size="100" /></td>
</tr>
<tr height="30px">
<td width="5%">Message :<br></td>
</tr>
<tr>
<table>
<tr height="30px">
<td width="5%"><textarea name="message" rows="20" cols="85"></textarea>"</td>
</tr>
</table>
</tr>
<tr>
<td><input type="Submit" value="Send"</td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
${err}
</body>
</html>
答案 0 :(得分:0)
我认为您没有设置所有强制属性来发送电子邮件,而mail.smpts.host是端口号。请将localhost更改为465,请检查以下属性是否在您的代码中
属性props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", 465);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
答案 1 :(得分:0)
您必须将电子邮件身份验证凭据设置为会话对象。请使用以下代码设置身份验证凭据
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourmail@gmail.com", "yourpassword");
}
});
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setContent(content, "text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emailid));
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, "yourmail@gmail.com", "yourpassword");
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
它应该有用。