我编写了这段index.jsp
代码,用于使用Gmail向其他人发送邮件。但这不起作用,请把这个错误告诉我。
“类型异常报告
messageInternal服务器错误
说明
服务器遇到内部错误,导致无法执行 完成此请求。
例外
javax.servlet.ServletException: javax.mail.internet.AddressException: Illegal address in string ``''
根本原因
javax.mail.internet.AddressException: Illegal address in string ``''
note异常及其根本原因的完整堆栈跟踪是
GlassFish
Server Open Source Edition 4.1.2日志中可用。 “。
<%--
Document : index
Created on : Mar 26, 2019, 7:48:36 PM
Author : ADITHYA
--%>
<%@page import="java.util.Properties"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sending an email</title>
</head>
<body onLoad="displayResult()">
<h1>Sending an email</h1>
<%!
public static class SMTPAuthenticator extends Authenticator{
public PasswordAuthentication getPasswordAuthentication (){
return new PasswordAuthentication("portaluniverses","xxxxx");
}
}
%>
<%
int result=0;
if(request.getParameter("send")!=null){
String d_uname="portaluniverses@gmail.com";
String d_password="xxxxx";
String d_host ="smtp.gmail.com";
int d_port=465;
String m_to=new String();
String m_from = "portaluniverses@gmail.com";
String m_subject=new String();
String m_text=new String();
if(request.getParameter("to")!=null){
m_to = request.getParameter("to");
}
if(request.getParameter("subject")!=null){
m_subject = request.getParameter("subject");
}
if(request.getParameter("message")!=null){
m_text="<h1>welcome to sasts </h1>";
m_text =m_text.concat(request.getParameter("message"));
m_text = m_text.concat("<br/><h2>hope you got link</h2>");
}
//creat property object
Properties props = new Properties();
//creat SMTPAuthentication object
SMTPAuthenticator auth =new SMTPAuthenticator();
//creat a mail sesion object
Session ses = Session.getInstance(props,auth);
//creat a MIME style email message object
MimeMessage msg =new MimeMessage(ses);
msg.setContent(m_text, "text/html");
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(m_from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
try{
Transport transport= ses.getTransport("SMTPS");
transport.connect(d_host,d_port, d_uname,d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
result=1;
}catch(Exception e){
out.println(e);
}
}
%>
<form name="myForm" action="index.jsp" method="POST">
<table border="0">
<tbody>
<tr>
<tr>To:</tr>
<tr> <input type="text" name="To" value="" /></tr>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject" value="" size="50" /></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message" rows="4" cols="50"></textarea> </td>
</tr>
</tbody>
</table>
<input type="hidden" name="hidden" value="<%= result %>" />
<input type="reset" value="Clear" name="Clear" />
<input type="submit" value="send" name="send" />
</form>
<script language="JavaScript">
function displayResult(){
if(document.myForm.hidden.value === "1"){
alert("Mail was sent");
}
}
</body>
</html>
答案 0 :(得分:0)
尝试使用此代码,
final String username = "portaluniverses@gmail.com";
final String password = "xxxxx";
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);
}
});
然后发送您的电子邮件,
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(messageText);
Transport.send(message);