I'm having troubles getting JavaMail to work with authentication, using an outside SMTP-server. The server is not the server of my ISP (the one which connects me to the internet now). I can use this SMTP-server to send mails with Thunderbird, so the connection should work.
I get the following error:
javax.mail.AuthenticationF
ailedExcep
tion
at javax.mail.Service.connect
(Service.j
ava:306)
at javax.mail.Service.connect
(Service.j
ava:156)
at javax.mail.Service.connect
(Service.j
ava:105)
at javax.mail.Transport.send0
(Transport
.java:168)
at javax.mail.Transport.send(
Transport.
java:98)
at net.grexx.square.mail.Simp
leSender.s
end(Simple
Sender.jav
a:66)
at net.grexx.square.mail.Proc
essSqlMail
.process(P
rocessSqlM
ail.java:6
7)
at org.apache.jsp.SimpleSende
r.test_002
ddb_jsp._j
spService(
org.apache
.jsp.Simpl
eSender.te
st_002ddb_
jsp:46)
at org.apache.jasper.runtime.
HttpJspBas
e.service(
HttpJspBas
e.java:99)
at javax.servlet.http.HttpSer
vlet.servi
ce(HttpSer
vlet.java:
802)
at org.apache.jasper.servlet.
JspServlet
Wrapper.se
rvice(JspS
ervletWrap
per.java:3
25)
at org.apache.jasper.servlet.
JspServlet
.serviceJs
pFile(JspS
ervlet.jav
a:295)
at org.apache.jasper.servlet.
JspServlet
.service(J
spServlet.
java:245)
at javax.servlet.http.HttpSer
vlet.servi
ce(HttpSer
vlet.java:
802)
at org.apache.catalina.core.A
pplication
FilterChai
n.internal
DoFilter(A
pplication
FilterChai
n.java:237
)
at org.apache.catalina.core.A
pplication
FilterChai
n.doFilter
(Applicati
onFilterCh
ain.java:1
57)
at org.apache.catalina.core.S
tandardWra
pperValve.
invoke(Sta
ndardWrapp
erValve.ja
va:214)
at org.apache.catalina.core.S
tandardCon
textValve.
invoke(Sta
ndardConte
xtValve.ja
va:178)
at org.apache.catalina.core.S
tandardHos
tValve.inv
oke(Standa
rdHostValv
e.java:126
)
at org.apache.catalina.valves
.ErrorRepo
rtValve.in
voke(Error
ReportValv
e.java:105
)
at org.apache.catalina.core.S
tandardEng
ineValve.i
nvoke(Stan
dardEngine
Valve.java
:107)
at org.apache.catalina.connec
tor.Coyote
Adapter.se
rvice(Coyo
teAdapter.
java:148)
at org.apache.coyote.http11.H
ttp11Proce
ssor.proce
ss(Http11P
rocessor.j
ava:825)
at org.apache.coyote.http11.H
ttp11Proto
col$Http11
Connection
Handler.pr
ocessConne
ction(Http
11Protocol
.java:731)
at org.apache.tomcat.util.net
.PoolTcpEn
dpoint.pro
cessSocket
(PoolTcpEn
dpoint.jav
a:526)
at org.apache.tomcat.util.net
.LeaderFol
lowerWorke
rThread.ru
nIt(Leader
FollowerWo
rkerThread
.java:80)
at org.apache.tomcat.util.thr
eads.Threa
dPool$Cont
rolRunnabl
e.run(Thre
adPool.jav
a:684)
at java.lang.Thread.run(Unkno
wn Source)
The java code that sends the mail is:
public static void send(String smtpServer, String smtpUsername, String smtpPassword, String to, String from
, String subject, String body)
{
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host"
, smtpServer);
props.put("mail.smtp.auth"
, "true");
Session session = Session.getDefaultInstance
(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.
RecipientT
ype.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.
RecipientT
ype.CC
// ,InternetAddress.parse(cc,
false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "GrexxBoxxEmail");
msg.setSentDate(new Date());
Transport tr = session.getTransport("smtp
");
tr.connect(smtpServer, smtpUsername, smtpPassword);
msg.saveChanges(); // don't forget this
// -- Send the message --
Transport.send(msg);
System.out.println("Messag
e sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
I have no idea what I'm doing wrong here. Any suggestions?
Start Free Trial