Hi all,
I am using open-source servlet to send email via SMTP. But the SMTP gateway requires authentication. Following is the error message :-
DEBUG: getProvider() returning javax.mail.Provider[TRANSP
ORT,smtp,c
om.sun.mai
l.smtp.SMT
PTransport
,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG: SMTPTransport trying to connect to host "smtp1.host.com", port 25
DEBUG SMTP RCVD: 220 mail1.host.com ESMTP Mirapoint 3.4.4-GR; Thu, 16 Sep 2004 17:49:19 +0800 (MYT)
DEBUG: SMTPTransport connected to host "smtp1.host.com", port: 25
DEBUG SMTP SENT: EHLO MYNAME
DEBUG SMTP RCVD: 250-mail1.host.com Hello MYNAME.host.com [1xx.1xx.2xx.2xx], pleased to meet you
250-8BITMIME
250-SIZE 13500000
250-DSN
250-ETRN
250-STARTTLS
250 HELP
DEBUG SMTP Found extension "8BITMIME", arg ""
DEBUG SMTP Found extension "SIZE", arg "13500000"
DEBUG SMTP Found extension "DSN", arg ""
DEBUG SMTP Found extension "ETRN", arg ""
DEBUG SMTP Found extension "STARTTLS", arg ""
DEBUG SMTP Found extension "HELP", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<eddie.see@host.com>
DEBUG SMTP RCVD: 530 <eddie.see@host.com>... MAIL requires AUTH
Messaging exception: 530 <eddie.see@host.com>... MAIL requires AUTH
==> How can I enable the servlet to send email via SMTP using authentication? The SMTP Gateway is "smtp1.host.com" but it seems to be re-routed to "mail1.host.com".
My source code is as follow:-
package com.elucify.tips.oct2002;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.MessagingExcept
ion;
import java.io.UnsupportedEncodin
gException
;
import java.util.Properties;
import javax.servlet.http.*;
import javax.servlet.*;
public class SendmailServlet extends HttpServlet {
protected ServletConfig _config;
protected Session _mailSession;
protected String _mailhost;
protected String _mailuser;
protected boolean _debug;
public SendmailServlet() {
_config = null;
_mailSession = null;
_mailhost = null;
_mailuser = null;
}
public void init(ServletConfig config) {
_config = config;
_mailhost = config.getInitParameter("m
ailhost");
_mailuser = config.getInitParameter("m
ailuser");
}
// doGet and doPost do the same thing
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
doService(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
doService(req, res);
}
protected void doService(HttpServletReque
st req,
HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/h
tml");
// Get (or create and cache) mail session
Session s = getMailSession();
// If either init parameter or request
// parameter "debug" is set, output
// SMTP session log to server log.
boolean debug = false;
String sdebug = _config.getInitParameter("
debug");
if (sdebug == null) {
sdebug = req.getParameter("debug");
}
if (sdebug != null &&
Boolean.valueOf(sdebug).bo
oleanValue
()) {
s.setDebug(true);
}
// Create new message
MimeMessage msg = new MimeMessage(s);
// Put data from request into message
try {
String messageText =
req.getParameter("messaget
ext");
msg.setText(messageText);
String subject = req.getParameter("subject"
);
if (subject != null) {
msg.setSubject(subject);
}
String password = req.getParameter("pwd");
String addrFrom =
req.getParameter("addrfrom
");
String nameFrom =
req.getParameter("namefrom
");
Address fromAddr =
new InternetAddress(addrFrom, nameFrom);
msg.setFrom(fromAddr);
String addrto = req.getParameter("addrto")
;
String nameto = req.getParameter("nameto")
;
Address toAddr =
new InternetAddress(addrto, nameto);
msg.addRecipient(Message.R
ecipientTy
pe.TO, toAddr);
// Send the message
//Transport.send(msg);
Transport tr = _mailSession.getTransport(
"smtp");
tr.connect(_mailhost, _mailuser, password);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
// Print a message acknowledging that the message
// was sent
PrintWriter pw = res.getWriter();
res.setContentType("text/h
tml");
pw.println("<HTML><BODY>Em
ail sent" +
"</BODY></HTML>");
} catch (UnsupportedEncodingExcept
ion e) {
System.err.println("Unsupp
orted encoding: " +
e.getMessage());
} catch (MessagingException m) {
System.err.println("Messag
ing exception: " +
m.getMessage());
}
}
// Open the mail session if it isn't already open.
protected Session getMailSession()
throws ServletException {
// Throw exception if mail host or user are not set
if (_mailhost == null || _mailuser == null) {
throw new ServletException("Sendmail
Servlet " +
"init parameters mailhost and mailuser must " +
"both be set in deployment descriptor");
}
// Create mail session if it doesn't exist
if (_mailSession == null) {
Properties p = new Properties();
p.put("mail.host", _mailhost);
p.put("mail.user", _mailuser);
p.put("mail.smtp.auth", "true");
// Can define and initialize other session
// properties here, if desired
_mailSession = Session.getDefaultInstance
(p, null);
}
return _mailSession;
}
}
Please help.
Eddie
Start Free Trial