uid = "<my email address>";
pswd = "<myPswd>";
SMTPAuthenticator auth = new SMTPAuthenticator(uid, pswd);
props.put("mail.smtp.host", "<myHost>");
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props,null);
but get error:
Session cannot be resolved to a type
how can I make this work in localhost please (you can imagine what a
pain it is to not be able to test email stuff on localhost..)
(localhost is Tomcat 5.5/jdk1.5.0_18)
would appreciate suggestions... thank you...
First, you do have a mailserver on some port active on your local
machine, right? The OS won't provide one. I was not aware that Tomcat
provided a mailserver.
see http://mindprod.com/jgloss/localhost.html
for some other ideas.
--
Roedy Green Canadian Mind Products
http://mindprod.com
"Everybody�s worried about stopping terrorism. Well, there�s a really easy way: stop participating in it."
~ Noam Chomsky
oh brother.. thank you Roedy..
I have the necessary jars for email in tomcat/common/lib
(activation.jar, imap.jar, mailapi.jar, pop3.jar, smtp.jar.. so Session
is working now.. had forgotten the jars..;) sorry... )
but it's still not working.. I would think having the necessary jars
should be all you need to send email from Tomcat locally, right?
esp since years ago I switched to commons mail API
(commons-email-1.0.jar) b/c of this problem, and SMTP authentication
worked locally with Tomcat; but now I have a bunch of email stuff in
JavaMail, and I really would like to not have to switch to commons again
just b/c of authentication problem.. it HAS to work in JavaMail..
I have done a lot of googling, one of the examples I found is here,
http://www.velocityreviews.com/forums/t136026-whats-the-easiest-way-to-send-email-with-smtp-authentication-using-java.html
however: this code includes "SMTPAuthenticator".... I don't see
SMTPAuthenticator type in JavaMail..
this is code I'm using now, that I also found online,
http://www.mayacove.com/misc/test_sendMail.txt
get error: authentication has failed...
thank you...
It appears from your exception that Tomcat cannot find the JavaMail jar.
Check to make sure that it is on the server where Tomcat can access it
or in your webapp's WEB-INF/lib directory.
--
Dave Miller
Java Web Hosting
http://www.cheap-jsp-hosting.com/
> this is code I'm using now, that I also found online,
> http://www.mayacove.com/misc/test_sendMail.txt
>
> get error: authentication has failed...
>
You're passing a null Authenticator. Properties are part of the mail
object. If authentication is required, it needs to succeed before the
object is passed.
Authentication is generally used for SMTP when the MTA is on a different
server than the client / sender. Examples would be GMail or when your
desktop client sends mail via your ISP.
You're using localhost as your MTA. Authentication on localhost is like
asking your C drive to authenticate itself to the D drive before a file
is copied. Unless your Tomcat is in a really bad neighborhood, your
service provider should not require authentication for local programs to
send mail via the MTA running specifically to handle their requests.
All that said, if authentication is required, you need to create an
authentication object to pass instead of null with:
Session l_session = Session.getInstance(l_props, null);
If auth is not required, lose the Authenticator object completely.
thank you.. now have:
Authenticator auth = new Authenticator();
Session l_session = Session.getInstance(l_props, auth);
get error: Cannot instantiate the type Authenticator
but in JavaMail docs
(http://java.sun.com/products/javamail/javadocs/index.html)
Authenticator IS a type...
(this is on a JSP.. most examples I have seen create a separate class
for authentication.. is it possible to do this w/o creating a class??
I do know servlets well, I just think they're a pain... this is for a
personal site w/not too much traffic..)
thank you...
Do something like these guys:
http://snippets.dzone.com/posts/show/3328
thank you for yr response, Dave.. my problem is I'm trying to do this
in a JSP.. you can't declare classes inside a JSP.. I've been
literally trying all day to find code to do SMTP authentication in a JSP
w/o having to declare a class.. the example you suggested I saw this
morning.. but how do I do this
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "auth-user";
String password = "auth-password";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
in a JSP???
thank you...
You don't - there's no method in javax.mail.Authenticator that does what
you want.
IMO, a JSP is not the right tool for the job it calls for a servlet. If
you must use a JSP, build a free standing class extending j.m.A and call
it from within the page.
ok.... thank you very much for your response.. yes I guess I will
have no choice but to do a servlet.. oh well....
maya wrote:
> ok.... thank you very much for your response.. yes I guess I will
> have no choice but to do a servlet.. oh well....
You make that sound like a bad thing.
JSPs are for the view, not for logic. Servlets are for control. POJOs are
for logic, under servlet control.
Remember that JSPs are themselves source code for servlets anyway. The
difference is that JSP source for servlets emphasizes presentation and layout;
Java source for servlets emphasizes control and logic, and de-emphasizes the view.
For what you have asked, control of a mail API, Java-sourced servlets will be
much easier to manage and maintain than JSP-sourced servlets. So doing that
with Java-sourced servlets will make your life easier. Oh, well.
--
Lew
One period suffices to signify the end of a sentence.
thank you Lew.. yes I know how JSPs work, that they get converted to
servlets, etc.. it's for my personal site which is small and has very
little traffic, and I wanted to avoid a servlet, just b/c you know, you
have to compile them and all that and it's just a pain, but well, that's
fine, the practice won't hurt me..;) (I compile in shell.. javac
-classpath.. etc.. have to include all jars for email (by the way,
what's activation.jar for? not 100% if I need it or not, I included it
in the classpath, but not sure if I do need it..)
thanks again..
I'm having difficulties w/this example..
http://snippets.dzone.com/posts/show/3328
I don't know where to put variables like
String sName = request.getParameter("name");
they have to go inside doGet() method b/c "request" object is declared
inside this method, but then it's not available to all other methods in
this example (ask me again why I didn't want to do this in a servlet....;)
:(
even if I do String sName = ""; outside doGet() method, and do
sName = request.getParameter("name");
inside doGet() method, it doesn't work..
if I do both String sName = ""; and
sName = request.getParameter("name");
inside doGet() method it still doesn't work... (I get "can't find
symbol" on all params I try to grab from request..) where do I
decl/init these variables in this example please, so they're available
to all methods...
thank you..
once again, thank you Lew, and to everyone else who responded.. finally
managed with this example:
http://zetcode.com/tutorials/jeetutorials/sendingemail/
(this example http://snippets.dzone.com/posts/show/3328 is a bit more
complicated than it needs to be, I think.. but what do I know...;)
well, here's a WEIRD problem I'm having with the servlet:
<input type="text" id="name" />
^^
sName = request.getParameter("name");
this does not work!!! param does not get passsed.. it only gets passed
if in form I do:
<input type="text" name="name" />
^^^^
this is totally bizarre, I did NOT have this problem in the JSP.. the
reason I need to do "id" and not "name" in the form is that all my stuff
has to validate as XHTML, and HTML code doesn't validate as XHTML if you
use "name" to identify elements (go figure.. who makes these stupid
decisions?? what's wrong with using "name" to identify an element?? esp
if using "id" doesn't work in some situations??)
this doesn't make any sense at all.. WHY am I having this problem with
the servlet but not with the JSP????
can anybody shed some light???
thank you....
That's because you asked for a parameter named "name" and there isn't
one - nothing weird about it (no matter how many exclamation points
you use). The 'name' attribute of the <input> tag is what identifies
parameters, not the 'id' attribute.
> it only gets passed if in form I do:
>
> Â Â <input type="text" name="name" />
> Â Â Â Â Â Â Â Â Â Â Â Â ^^^^
>
> this is totally bizarre, I did NOT have this problem in the JSP.. the
It's neither bizarre nor a problem - the parameters you get from
'request.getParameter()' by definition are those that have names given
by the attribute 'name', not by the attribute 'id'.
> reason I need to do "id" and not "name" in the form is that all my stuff
> has to validate as XHTML, and HTML code doesn't validate as XHTML if you
> use "name" to identify elements (go figure.. who makes these stupid
> decisions?? what's wrong with using "name" to identify an element?? esp
> if using "id" doesn't work in some situations??)
>
You still have to use 'name' to identify parameters. Identification
of elements and identification of parameters are different things.
The 'id' attribute identifies elements for XML; it has nothing to do
with passing parameters. For that you need the 'name' attribute.
> this doesn't make any sense at all.. Â WHY am I having this problem with
> the servlet but not with the JSP????
>
One question mark suffices to indicate an interrogative.
I don't know why the JSP works differently, but if you don't have a
'name' attribute on your parameters, you're doing it wrong for
servlets even if they do have JSP for source.
'getParameter()' works only with parameters that were given a name
with the 'name' attribute. If you look at
<http://w3schools.com/tags/att_input_name.asp>
it documents that the 'name' attribute "is used to identify form data
after it has been submitted to the server" and that "[o]nly form
elements with a name attribute will have their values passed when
submitting a form".
That has not changed between pre-X HTML and XHTML.
--
Lew
>I don't know why I can't make SMTP server authentication work with
>JavaMail.. code:
I have posted code for a working app the does most of the JavaMail
functions. Feel free to cannibalise.
See http://mindprod.com/products1.html#BULK
oh brother... it seems the more I work on this the more trouble I run
into; here's a really weird one: SMTP code:
props.put("mail.smtp.host", "smtp.comcast.net");
props.setProperty("mail.smtp.port", "587");
Authenticator auth = new SMTPAuthenticator(uid, Pswd);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props, auth);
yet when I try to send email from localhost I get following error:
Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
I don't get this.. would appreciate some suggestions..
thank you...
[...]
> oh brother... it seems the more I work on this the more trouble I run
> into; here's a really weird one: SMTP code:
>
> props.put("mail.smtp.host", "smtp.comcast.net");
> props.setProperty("mail.smtp.port", "587");
> Authenticator auth = new SMTPAuthenticator(uid, Pswd);
> props.setProperty("mail.smtp.auth", "true");
> Session session = Session.getInstance(props, auth);
>
>
> yet when I try to send email from localhost I get following error:
>
> Could not connect to SMTP host: localhost, port: 25;
> nested exception is:
> java.net.ConnectException: Connection refused: connect
>
>
> I don't get this.. would appreciate some suggestions..
On which port is your SMTP server running: 25 or 587, and why don't the
property & error ports match? The netstat coommand may be helpful here.
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
well, for Comcast it has to be port 587 (this is what I use from my
email client also, it works fine..) it was working fine last night for a
bit, then all of a sudden I started getting that weird error...
netstat? I don't know what that is.. thank you for your help...
You might look in your version control system for changes around the
same time. Also, your ISP may have reacted adversely to your activity.
> netstat? I don't know what that is.. thank you for your help...
<http://www.manpagez.com/man/1/netstat/>
Is the servlet running on a computer that sits on Comcast's network? If
so, they are likely accepting connections on 25 from you.
If the servlet is running on a computer that's not physically on
Comcast's network, you might be (likely are) getting caught up in their
anti spam work. Good luck getting past that on a ongoing basis. Also
begs the question "why aren't you using localhost or your network's MTA?".
All that said, the exception means exactly what it says - your
connection is being refused. If you're on their network, try removing
the port declaration. If not, you probably need to use a different host.
yes I suspect Comcast has a problem with me sending email from their
SMTP programmatically, I suspected as much before I even started doing
this (and since it worked briefly last night and then it didn't, they
probably put a stop to it mighty fast..) oh brother.. and I can't do it
from my own SMTP (from my domain/webhosting.. can't even send email from
my email client from my webhosting SMTP.. they said Comcast probably
blocks it..)
oh man.. so much for this.. I think I will have to give up on sending
email from my localhost (i.e., Tomcat installed on my machine)
programmatically... I just wanted the ability to test my email stuff
locally before pushing to "production".. oh well.. (not sure what you
mean by is my machine on Comcast's network, they are my ISP, my Internet
broadband provider, I don't know if technically this means my machine is
"on their network"..)
oh well.. I tried.. thank you very much to everyone for their help.
If Comcast provides the connection to the Internet from the computer on
which you're running the servlet, you are on their network.
Remove:
props.setProperty("mail.smtp.port", "587");
and the code should work.
>
>
>
> oh brother... it seems the more I work on this the more trouble I run
> into; here's a really weird one: SMTP code:
>
> props.put("mail.smtp.host", "smtp.comcast.net");
> props.setProperty("mail.smtp.port", "587");
> Authenticator auth = new SMTPAuthenticator(uid, Pswd);
> props.setProperty("mail.smtp.auth", "true");
> Session session = Session.getInstance(props, auth);
>
>
> yet when I try to send email from localhost I get following error:
>
> Could not connect to SMTP host: localhost, port: 25;
> nested exception is:
> java.net.ConnectException: Connection refused: connect
>
>
You need to review exactly what you are doing. The code and the error message
are self-contradictory. The code is attempting to contact smtp.comcast.net on
port 587, but the error messages says that an attempt contact localhost on port
25 failed. I don't see how the former could possibly lead to the latter, unless
you have some really, really weird port redirecting/NAT firewall configuration.
--
Nigel Wade
> maya wrote:
> (not sure what you
>> mean by is my machine on Comcast's network, they are my ISP, my Internet
>> broadband provider, I don't know if technically this means my machine is
>> "on their network"..)
>
> If Comcast provides the connection to the Internet from the computer on
> which you're running the servlet, you are on their network.
>
> Remove:
>
> props.setProperty("mail.smtp.port", "587");
>
> and the code should work.
>
Port 25 is normally used by MTAs for mail submission and transfer without
authentication. Authentication is normally done on port 587. Although it is
possible to offer authentication on port 25. There is a simple way to test it
out, telnet to port 25 and type "EHLO some.domain.name". The response will tell
you what the MTA supports. If it doesn't include AUTH then AUTH won't work on
port 25. You should be able to try the same thing on port 587, port 587 should
not be encrypted at connection.
There is one situation where this won't work. That is if the mail server doesn't
offer AUTH unless encryption is already established. This cannot be tested
using telnet because you won't be able to establish an SSL encrypted connection
using TLS over telnet.
I can't test it from here because smtp.comcast.net does not respond on port 587
indicating it's probably an internal only submission system. I can't test port
25 because that is blocked outgoing here. Also that host has no MX records
which mean it's unlikely to be an MTA and probably doesn't listen on port 25,
certainly not to the outside world anyway.
--
Nigel Wade
thank you Nigel.. I suspect that since Comcast put a stop to my
attempts the connection somehow "defaulted" back to port 25...
>> maya wrote:
>>
>>>
>>> yet when I try to send email from localhost I get following error:
>>>
>>> Could not connect to SMTP host: localhost, port: 25;
>>> nested exception is:
>>> java.net.ConnectException: Connection refused: connect
>>>
>
> thank you Nigel.. I suspect that since Comcast put a stop to my
> attempts the connection somehow "defaulted" back to port 25...
I suspect that Comcast did no such thing (well, they might have, but that would
not be the cause of this particular problem).
Somewhere in your code you are attempting to connect to port 25 on *your*
machine (localhost), this has nothing to do with what Comcast may or may not
have done. localhost uses the loopback interface, connecting to Comcast uses
your Ethernet interface. There is nothing Comcast could possibly do to redirect
to a different port on the loopback interface. That would have to happen within
the kernel of your machine (the loopback interface is entirely internal to the
system), or your code is broken and not doing what you think it is.
--
Nigel Wade
> Port 25 is normally used by MTAs for mail submission and transfer without
> authentication. Authentication is normally done on port 587. Although it is
> possible to offer authentication on port 25. There is a simple way to test it
> out, telnet to port 25 and type "EHLO some.domain.name". The response will tell
> you what the MTA supports. If it doesn't include AUTH then AUTH won't work on
> port 25. You should be able to try the same thing on port 587, port 587 should
> not be encrypted at connection.
>
> There is one situation where this won't work. That is if the mail server doesn't
> offer AUTH unless encryption is already established. This cannot be tested
> using telnet because you won't be able to establish an SSL encrypted connection
> using TLS over telnet.
>
> I can't test it from here because smtp.comcast.net does not respond on port 587
> indicating it's probably an internal only submission system. I can't test port
> 25 because that is blocked outgoing here. Also that host has no MX records
> which mean it's unlikely to be an MTA and probably doesn't listen on port 25,
> certainly not to the outside world anyway.
>
Most ISP's have gone to auth on submit to combat spam. They all block 25
from outside their networks for the same reason. We use a Comcast
competitor (Verizon) which uses auth on 25 intra network only.
I also don't have access to Comcast's network so this is only a guess
but they have something north of 15 million customers the vast majority
of whom are home users. It would be a tech support nightmare of the
living dead to try to migrate those users from 25 to 587 so I'm thinking
that they listen on 25 for customers physically connected to their network.
well, again, for sending email from my email client thru Comcast SMTP
server it's port 587, so I assumed it was also port 587 for sending
email from a program (and, as I mentioned previously, it worked fine for
a few minutes, after I had finally gotten all "my ducks in a row"
code-wise....;) but it only worked for a few mins, then I started
getting that "port 25" error.. this is not a major issue for me
personally (but at least I now learned how to do this..) it's not a
major necessity for me to be able to send email from my localhost, but
if it were I would be in big doo-doo.. this is not very good.. (I can't
use SMTP from my webhosting, Comcast blocks it.. not sure I like all
these restrictions.. they cause more nuissance than anything else for
home users like me, and I don't think they REALLY combat spam.. spammers
have plenty of tricks and devices up their sleeve either way... don't
they...? I'm sure heavy-duty spammers have their own SMTP servers,
thank you very much....;)
The SMTP protocol doesn't differentiate between "submission" and "transport".
Submission is simply the originating source of the message. Transport/relay
uses exactly the same protocol, and the receiving MTA can't reliably determine
the difference between a mail client initiating a message and another MTA
relaying that message.
Many/most ISPs now differentiate mail submission by their own customers from
mail transport from other domains by using different hosts to perform these two
separate tasks. They will have configured their firewalls and authentication
schemes accordingly. The machines handling internal mail "submission" by their
own customers will almost certainly require authentication, and most likely
encryption, and not be accessible to the outside world.
>
> I also don't have access to Comcast's network so this is only a guess
> but they have something north of 15 million customers the vast majority
> of whom are home users. It would be a tech support nightmare of the
> living dead to try to migrate those users from 25 to 587 so I'm thinking
> that they listen on 25 for customers physically connected to their network.
>
An ISP must allow incoming SMTP without authentication on port 25 to their mail
servers, or they won't be able to receive any incoming mail from other domains.
They can easily block their own customers from talking directly to their
Internet facing mail servers because they route all the traffic. To provide
those customers with port 25 submission the ISP only has to provide an internal
MTA, which may require auth. However, it's a different matter entirely to block
other ISPs customers.
They will probably implement SPF, or some other hackish attempt to circumvent
spam, but these generally either block legitimate mail or impose more strain
and overhead on other servers. They will almost certainly attempt to block
connection from a dynamic IP and will probably refuse any connection from an IP
which doesn't have correct rDNS and appropriate MX records.
My guess, and it's only a guess, is that the machine which the OP is attempting
to connect to is a Comcast internal mail submission system for Comcast
customers. This will most likely relay the mail via Comcast's mail servers
otherwise other ISPs with similar mail blocking schemes would not receive it.
Comcast do have a reputation for blocking mail from other ISPs due their
misconfiguration (or conspiracy if you prefer that theory).
--
Nigel Wade
I don't have answers for how you do this through Java but I've had
some (bad) experiences with Comcast and sendmail. I have something
that may be of interest to you using Gmail:
http://www.linuxha.com/other/sendmail/gmail.html
--
Linux Home Automation Neil Cherry nch...@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies