使用 Java 邮件 api 使用 gmail smtp 从本地主机发送邮件时出现空指针异常。
Getting Null pointer Exception while using the Java mail api to send mail from local host using gmail smtp.
在 servlet 中 class 我正在初始化 init 方法中的所有变量
private String host="smtp.gmail.com";
private String port="465";
private String user="abc@gmail.com";
private String pass="pass";
public void init()
{
// reads SMTP server setting from web.xml file
ServletContext context = getServletContext();
host = context.getInitParameter("host");
port = context.getInitParameter("port");
user = context.getInitParameter("user");
pass = context.getInitParameter("pass");
}
在 EmailUtility class 我有下面的代码
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Authenticator auth = new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport.send(msg);
异常堆栈跟踪是
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at EmailUtility.sendEmail(EmailUtility.java:31)
at EmailSendingServlet.doPost(EmailSendingServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我正在尝试使用 gmail smtp 在本地主机上测试此邮件发送机制。无法跟踪此异常发生的原因。我也尝试通过更改端口号来 运行 这个,但最终遇到了同样的问题。
host
回来了 null
。由于 Properties
扩展了 Hashtable
,您正在使用 Hashtable 的 put
方法。但是,put
will throw an NPE if either the key or value are null
:
Throws:
NullPointerException
- if the key or value is null
确保您从 context.getInitParameter
的所有调用中获取一个值,并且它们正在获取正确的变量。
在 servlet 中 class 我正在初始化 init 方法中的所有变量
private String host="smtp.gmail.com";
private String port="465";
private String user="abc@gmail.com";
private String pass="pass";
public void init()
{
// reads SMTP server setting from web.xml file
ServletContext context = getServletContext();
host = context.getInitParameter("host");
port = context.getInitParameter("port");
user = context.getInitParameter("user");
pass = context.getInitParameter("pass");
}
在 EmailUtility class 我有下面的代码
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Authenticator auth = new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport.send(msg);
异常堆栈跟踪是
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at EmailUtility.sendEmail(EmailUtility.java:31)
at EmailSendingServlet.doPost(EmailSendingServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我正在尝试使用 gmail smtp 在本地主机上测试此邮件发送机制。无法跟踪此异常发生的原因。我也尝试通过更改端口号来 运行 这个,但最终遇到了同样的问题。
host
回来了 null
。由于 Properties
扩展了 Hashtable
,您正在使用 Hashtable 的 put
方法。但是,put
will throw an NPE if either the key or value are null
:
Throws:
NullPointerException
- if the key or value is null
确保您从 context.getInitParameter
的所有调用中获取一个值,并且它们正在获取正确的变量。