How to Send Mail from JavaMail via Localhost
I am creating a form, which will send out the details via email upon user completes his details and click submit.
Mail Submission with JavaMail:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String host = "localhost";
String from = "root@localhost.localdomain";
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Transport transport = session.getTransport("smtp");
MimeMessage message = new MimeMessage(session);
Address fromAddress = new InternetAddress("root@localhost.localdomain");
message.setFrom(fromAddress);
InternetAddress to = new InternetAddress("sendToAliases@localhost.localdomain");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Email Details Sending");
message.setText("This is my testing content.");
transport.connect(host, from);
message.saveChanges();
Transport.send(message);
transport.close();
} finally {
out.close();
}
}
I am using Email aliases for sendToAliases@localhost.localdomain which means 开发者_如何学运维I could have 4 email aliases from sendToAliases. However, I am unable to reach any emails upon deploying and running the above mail file. Can anyone please advise me?
Thank you.
- Have you checked the log files?
- Do you get any exceptions or errors when running the program?
- Do you have a SMTP server running in localhost?
- Is the SMTP server accepting connections from localhost?
- Can you send emails via that server using normal email client and receive them somehow?
- Try to make your program a standalone commandline program and try to execute it
You seem to have a missing quote in message.setSubject("Email Details Sending);
. Are you sure that your servlet actually compiles?
精彩评论