• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javamail authenticator

K McGinley
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get a JSP to send email based on form data submitted from an html page. I'm approaching this the same way as I would with a regular application, but I don't know how to incorporate the authentication to an outgoing mail server. I'm new to JSP, this is actually my first one. Please let me know of a way to use my authentication class to connect to a mail server, or if there is another way it should be done. The code is listed below. Thanks.
userid, passwd, and host undisclosed for obvious reasons.
my authentication class:
public class MyAuthenticator extends Authenticator {
private String userid = "xxuseridxx";
private String passwd = "xxpasswordxx";

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userid, passwd);

my jsp:
<%@page contentType="text/html"%>
<head><title>Email confirmation</title></head>
<%@ page import="java.util.Properties, javax.mail.*, javax.mail.internet.*" %>
String host = "xxsmtpxx";
String from = request.getParameter("sender");
String to = request.getParameter("recipient");
String subject = request.getParameter("subject");
String messagecontent = request.getParameter("message");
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Authenticator authi = new MyAuthenticator();
Session mailsession = Session.getDefaultInstance(props, authi);
MimeMessage emailmessage = new MimeMessage(mailsession);
emailmessage.setFrom(new InternetAddress(from));
emailmessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

out.println("Your message has been successfully sent to " + to + " at [time]");
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic