使用Java Netbeans发送电子邮件(例外)
最近我制作了一个发送电子邮件的web应用程序。代码运行良好,我没有错误,我有运行时异常使用Java Netbeans发送电子邮件(例外),java,ssl,jakarta-mail,Java,Ssl,Jakarta Mail,最近我制作了一个发送电子邮件的web应用程序。代码运行良好,我没有错误,我有运行时异常 (javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty) 我的index.jsp代码是: <%@page content
(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>
请帮助我,我总是有运行时异常:
异常javax.mail.MessaginException:无法将套接字转换为TLS;
嵌套异常是:
javax.net.ssl.SSLException:java.lang.RuntimeException:意外错误:java.security.invalidalgorithParameterException:trustAnchors参数必须为非空
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
通过SSL使用gmail SMTP这是我的代码
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
public static int Send(String SMTPServer,
String Sender,
String Recipient,
String Subject,
String Body,
String ErrorMessage,
String Attachments) {
// Error status;
int ErrorStatus = 0;
// Create some properties and get the default Session;
final String username = Sender;
final String password = "pswd";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a message.
MimeMessage msg = new MimeMessage(session);
// extracts the senders and adds them to the message.
// Sender is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}
// Extract the recipients and assign them to the message.
// Recipient is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}
// Subject field
msg.setSubject(Subject);
// Create the Multipart to be added the parts to
Multipart mp = new MimeMultipart();
// Create and fill the first message part
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);
// Attach the part to the multipart;
mp.addBodyPart(mbp);
}
// Attach the files to the message
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))) {
// Create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex,PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// Last, or only, attachment file;
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}
// Add the Multipart to the message
msg.setContent(mp);
// Set the Date: header
msg.setSentDate(new Date());
// Send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
System.out.println("blows here");
ErrorMessage = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) != null)
ErrorMessage += "\n" + TheException.toString();
ErrorStatus = 1;
}
System.out.println(ErrorMessage);
return ErrorStatus;
} // End Send method
public static void main(String args[]){
SendMail.Send("smtp.gmail.com", "my_mail@gmail.com", "my_mail@gmail.com",
"try sending mail", "came? ","" ,null);
}
} // End of public class SendMail
import java.util.Date;
导入java.util.Properties;
导入javax.activation.DataHandler;
导入javax.activation.FileDataSource;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart;
公共类SendMail{
公共静态int发送(字符串SMTPServer,
字符串发送器,
字符串收件人,
字符串主题,
弦体,
字符串错误消息,
字符串(附件){
//错误状态;
int ErrorStatus=0;
//创建一些属性并获取默认会话;
最终字符串用户名=发送者;
最终字符串password=“pswd”;
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.port”,“587”);
会话=会话.getInstance(props,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
试一试{
//创建一条消息。
MimeMessage msg=新MimeMessage(会话);
//提取发件人并将其添加到邮件中。
//根据RFC822,发件人是以逗号分隔的电子邮件地址列表。
{
InternetAddress[]TheAddress=InternetAddress.parse(发送方);
msg.addFrom(地址);
}
//提取收件人并将其分配给邮件。
//根据RFC822,收件人是以逗号分隔的电子邮件地址列表。
{
InternetAddress[]TheAddress=InternetAddress.parse(收件人);
msg.addRecipients(Message.RecipientType.TO,地址);
}
//主题字段
msg.setSubject(主题);
//创建要添加零件的多部分
Multipart mp=新的MimeMultipart();
//创建并填充第一个消息部分
{
MimeBodyPart mbp=新的MimeBodyPart();
mbp.setText(正文);
//将部件连接到多部件;
mp.addBodyPart(mbp);
}
//将文件附加到邮件
如果(空!=附件){
int StartIndex=0,PosIndex=0;
而(-1!=(PosIndex=Attachments.indexOf(“///”,StartIndex))){
//创建和填充其他消息部分;
MimeBodyPart mbp=新的MimeBodyPart();
文件数据源fds=
新的FileDataSource(Attachments.substring(StartIndex,PosIndex));
setDataHandler(新DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex+=3;
StartIndex=PosIndex;
}
//最后一个或仅一个附件文件;
if(StartIndex
对不起,缩进
现在试试。希望它能起作用
这是邮件的输出。在我的电脑里,它发送信息
您的jre中是否安装了可信证书?看看这个我以前看过这个,我什么都不懂,请告诉我我能做什么,我应该遵循什么步骤?您使用的是哪个SMTP服务器?服务器:SMTP.gmail.com我尝试过这个,现在我没有例外,但是页面加载太多,什么也没发生。你收到邮件了,什么都没发生是什么意思?我正在给我的另一封邮件发送一封邮件。单击“发送”时,页面正在加载,既没有异常也没有输出。正在加载网页。您的系统输出是否从主类打印出来?您需要确定您的请求得到了成功处理。@user2284478:您需要更改代码,但这至少会发送邮件。您可以上传项目吗?没有main方法>。它不起作用吗?我刚刚编辑了代码。写下你的邮件地址
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
public static int Send(String SMTPServer,
String Sender,
String Recipient,
String Subject,
String Body,
String ErrorMessage,
String Attachments) {
// Error status;
int ErrorStatus = 0;
// Create some properties and get the default Session;
final String username = Sender;
final String password = "pswd";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a message.
MimeMessage msg = new MimeMessage(session);
// extracts the senders and adds them to the message.
// Sender is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}
// Extract the recipients and assign them to the message.
// Recipient is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}
// Subject field
msg.setSubject(Subject);
// Create the Multipart to be added the parts to
Multipart mp = new MimeMultipart();
// Create and fill the first message part
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);
// Attach the part to the multipart;
mp.addBodyPart(mbp);
}
// Attach the files to the message
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))) {
// Create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex,PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// Last, or only, attachment file;
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}
// Add the Multipart to the message
msg.setContent(mp);
// Set the Date: header
msg.setSentDate(new Date());
// Send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
System.out.println("blows here");
ErrorMessage = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) != null)
ErrorMessage += "\n" + TheException.toString();
ErrorStatus = 1;
}
System.out.println(ErrorMessage);
return ErrorStatus;
} // End Send method
public static void main(String args[]){
SendMail.Send("smtp.gmail.com", "my_mail@gmail.com", "my_mail@gmail.com",
"try sending mail", "came? ","" ,null);
}
} // End of public class SendMail