最近我做了一个发送电子邮件的网络应用程序。代码工作正常我没有错误,我有运行时异常
(javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)
我的 index.jsp 代码是:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Java Mail </title>
</head>
<body>
<form action="sendMail.jsp" method="POST">
<table border="0" align="center" cellpadding="5">
<tbody>
<thead><tr> <td colspan="3" align="center">
<b> Send Mail </b> </td> </tr> </thead>
<tr>
<td> To </td> <td> : </td>
<td> <input type="text" name="to" value="" /> </td>
</tr>
<tr>
<td> Subject </td> <td> : </td>
<td> <input type="text" name="subject" value="" /> </td>
</tr>
<tr>
<td> Message </td> <td> : </td>
<td> <textarea name="message" rows="8" cols="30">
</textarea></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="Send Mail" />
<input type="reset" value="Reset" />
<td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
我的 sendMail.jsp 代码是:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=">
<jsp:useBean id="mail" scope="session" class="jMail.Mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="MyEmail@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
</head>
<body>
<%
String to = mail.getTo();
int result;
result = mail.sendMail();
if (result == 0) {
out.println(" Mail Successfully Sent to " + to);
} else {
out.println(" Mail NOT Sent to " + to);
}
%>
</body>
</html>
我的 Mail.java 代码是:
package jMail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail {
private String to;
private String from;
private String message;
private String subject;
private String smtpServ;
/**
* @return the to
*/
public String getTo() {
return to;
}
/**
* @param to the to to set
*/
public void setTo(String to) {
this.to = to;
}
/**
* @return the from
*/
public String getFrom() {
return from;
}
/**
* @param from the from to set
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the subject
*/
public String getSubject() {
return subject;
}
/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return the smtpServ
*/
public String getSmtpServ() {
return smtpServ;
}
/**
* @param smtpServ the smtpServ to set
*/
public void setSmtpServ(String smtpServ) {
this.smtpServ = smtpServ;
}
public int sendMail(){
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true" );
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(message);
// -- Set some other header information --
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent to"+to+" OK." );
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "MyEmail@gmail.com";
String password = "MyPassword";
return new PasswordAuthentication(username, password);
}
}
}
请帮助我我总是有运行时异常:
异常 javax.mail.MessagingException:无法将套接字转换为 TLS;嵌套异常是:javax.net.ssl.SSLException:java.lang.RuntimeException:意外错误:java.security.InvalidAlgorithmParameterException:trustAnchors 参数必须为非空