javamail电子邮件不发送
Posted
技术标签:
【中文标题】javamail电子邮件不发送【英文标题】:javamail email not send 【发布时间】:2012-04-27 10:30:36 【问题描述】:我正在尝试使用 javamail 发送电子邮件,但我是这个主题的新手。到目前为止,我没有收到编译错误或运行错误,但电子邮件从未发送。我使用一个帮助类来发送带有本地 SMTP 服务器(MailUtilLocal.java)和发送电子邮件的 servlet(EmailServlet)的电子邮件。代码如下:
` //EmailServlet.java:
import Beans.Usuario;
import IOBD.InputOutputBD;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.MessagingException;
import Servlets.MailUtilLocal;
import java.util.*;
public class EmailServlet extends HttpServlet
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
// get parameters from the request
String userId = request.getParameter("userId");
String password = request.getParameter("password");
String nombre = request.getParameter("nombre");
String tipo = request.getParameter("tipo");
String emailAddress = request.getParameter("emailAddress");
// create the User object and write it to a file
Usuario user = new Usuario(userId, password, nombre, tipo, emailAddress);
ServletContext sc = getServletContext();
// store the User object in the session
HttpSession session = request.getSession();
session.setAttribute("user", user);
// send email to user
String to = emailAddress;
String from = "email_list@murach.com";
String subject = "Welcome to our email list";
String body = "Dear " + nombre + ",\n\n"
+ "Thanks for joining our email list. "
+ "We'll make sure to send you announcements "
+ "about new products and promotions.\n"
+ "Have a great day and thanks again!\n\n"
+ "Kelly Slivkoff\n" + "Mike Murach & Associates";
boolean isBodyhtml = false;
try
MailUtilLocal.sendMail(to, from, subject, body, isBodyHTML);
catch (MessagingException e)
String errorMessage =
"ERROR: Unable to send email. "
+ "Check Tomcat logs for details.<br>"
+ "NOTE: You may need to configure your "
+ "system as described in chapter 15.<br>"
+ "ERROR MESSAGE: " + e.getMessage();
request.setAttribute("errorMessage", errorMessage);
this.log("Unable to send email. \n"
+ "Here is the email you tried to send: \n"
+ "=====================================\n"
+ "TO: " + emailAddress + "\n"
+ "FROM: " + from + "\n"
+ "SUBJECT: " + subject + "\n"
+ "\n"
+ body + "\n\n");
// forward request and response to JSP page
String url = "/index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
//MailUtilLocal.java:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailUtilLocal
public static void sendMail(String to, String from, String subject, String body, boolean bodyIsHTML)
throws MessagingException
// 1 - get a mail session
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", 25);
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
// 2 - create a message
Message message = new MimeMessage(session);
message.setSubject(subject);
if (bodyIsHTML)
message.setContent(body, "text/html");
else
message.setText(body);
// 3 - address the message
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(to);
message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 4 - send the message
Transport.send(message);
我删除了这两行,因为在 UserIO 上没有方法 addRecord 时出现错误,并且我不知道其中应该包含什么
String path = sc.getRealPath("/WEB-INF/EmailList.txt");
UserIO.addRecord(user, path);
【问题讨论】:
您应该使用像 common email 或 spring email 这样的帮助库,而不是编写样板代码。其次,你没有捕捉到所有的异常,可能有一个没有被注意到。 为什么在这行代码props.put("mail.smtp.host", "localhost");
中指定localhost。您需要在此处指定特定的邮件服务器。
@Lion OP 提到它是一个本地邮件服务器。
哦,读到这我都困了!
JavaMail FAQ 有一些调试技巧。
【参考方案1】:
当您使用本地 smtp 服务器时,有时会发生的情况是您的 ISP 会阻止端口 25,因此您的 SMTP 服务器无法与远程服务器通信。
在这种情况下,您可以使用不同的端口从您的 smtp 服务器发送电子邮件,或使用接受来自 25 以外的端口(通常是 587 和 465)的连接的第三方服务。
看看Sendgrid、mailjet和postmarkapp,因为它们是一些最知名的提供商
【讨论】:
以上是关于javamail电子邮件不发送的主要内容,如果未能解决你的问题,请参考以下文章