mehdi
asked on
Sending Email with Java
Ok, I cant send email.. there !
well, a bit more complicated, im using a "mailbean" written by me.. anyway, i dev the code on my local machine, and i get it working !.. cool,
next thing that happens is that i upload all my classes, jsp's, a tweak here and there.. everything is working.. except, no email ! no errors are reported so I dont know whats up.. im using the same SMTP address as on my local box, email addresses are the same..
I think i may be missing some classes needed to send the emails.. in my WEB-INF/classes directiry i have
java
javax
oeg
sun
and all their sub directories / classes.
So what else do i need to stick in here ?
btw, me new to Java :)
well, a bit more complicated, im using a "mailbean" written by me.. anyway, i dev the code on my local machine, and i get it working !.. cool,
next thing that happens is that i upload all my classes, jsp's, a tweak here and there.. everything is working.. except, no email ! no errors are reported so I dont know whats up.. im using the same SMTP address as on my local box, email addresses are the same..
I think i may be missing some classes needed to send the emails.. in my WEB-INF/classes directiry i have
java
javax
oeg
sun
and all their sub directories / classes.
So what else do i need to stick in here ?
btw, me new to Java :)
check the log files .. your bean probably can't find the SMTP server or there is some other error
Here is a SendMail method that we have working in produciton. Thre imported classes for this class file are:
import java.util.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public boolean sendMail(String from, String to[], String subject, String msg)
{
if(from==null||to==null||s
//java.lang.System.out.pri
return false;
}
else {
String email_to_addresses = "";
for(int i = 0; i < to.length -1; i++)
{
email_to_addresses = email_to_addresses + to[i];
email_to_addresses = email_to_addresses + ",";
}
email_to_addresses = email_to_addresses + to[to.length - 1];
Properties properties = new Properties();
properties.put("smtp2.comp
Session session1 = Session.getDefaultInstance
MimeMessage mimemessage = new MimeMessage(session1);
try
{
InternetAddress ainternetaddress[] = null;
ainternetaddress = InternetAddress.parse(emai
mimemessage.setRecipients(
mimemessage.setFrom(new InternetAddress(from));
mimemessage.setSubject(sub
mimemessage.setContent(msg
Transport.send(mimemessage
}
catch(Exception exception)
{ return false; }
}
return true;
}
You should be able to change the names and test it out. I assume you know how to get back the error messages, if any.
I'll bet you're missing activation.jar, because it's not intuitive that it's part of the Java Mail API.
There is a sample program in Java Mail which uses a properties file, and one of the settings is debug=true(false). I have found that very helpful in diagnosing problems when moving code from my environment to another server.
Dorothy
There is a sample program in Java Mail which uses a properties file, and one of the settings is debug=true(false). I have found that very helpful in diagnosing problems when moving code from my environment to another server.
Dorothy
Yes - u need both mail.jar and the activation.jar as Dorothy said.
If the advice did not help u, would u post the exceptions or something?
If the advice did not help u, would u post the exceptions or something?
Agreed, send your code out here for review.
ASKER
Ok folks, i appreciate the feedback.. I got the exception :)
Here it is:
14-Sep-01 15:41:33: javax.mail.NoSuchProviderE xception: No provider for Address type: rfc822
14-Sep-01 15:41:33: Stack trace: javax.mail.NoSuchProviderE xception: No provider for Address type: rfc822
at javax.mail.Session.getTran sport(Sess ion.java:4 75)
at javax.mail.Transport.send0 (Transport .java:154)
at javax.mail.Transport.send( Transport. java:80)
at com.bluebase.mail.MailBean .emailProc ess(MailBe an.java:58 )
at com.bluewave.mayflower.req uesthandle rs.MFLMail FormUser.h andleReque st(MFLMail FormUser.j ava:73)
at com.bluebase.model2.Reques tControlle r.getNextP age(Reques tControlle r.java:74)
at _0002fcontroller_0002ejspc ontroller_ jsp_0._jsp Service(_0 002fcontro ller_0002e jspcontrol ler_jsp_0. java:132)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:119 )
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.jasper.servlet. JspServlet $JspCounte dServlet.s ervice(Jsp Servlet.ja va:130)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.jasper.servlet. JspServlet $JspServle tWrapper.s ervice(Jsp Servlet.ja va:282)
at org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:429)
at org.apache.jasper.servlet. JspServlet .service(J spServlet. java:500)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.tomcat.core.Ser vletWrappe r.doServic e(ServletW rapper.jav a:405)
at org.apache.tomcat.core.Han dler.servi ce(Handler .java:287)
at org.apache.tomcat.core.Ser vletWrappe r.service( ServletWra pper.java: 372)
at org.apache.tomcat.core.Con textManage r.internal Service(Co ntextManag er.java:81 2)
at org.apache.tomcat.core.Con textManage r.service( ContextMan ager.java: 758)
at org.apache.tomcat.service. connector. Ajp12Conne ctionHandl er.process Connection (Ajp12Conn ectionHand ler.java:1 66)
at org.apache.tomcat.service. TcpWorkerT hread.runI t(PoolTcpE ndpoint.ja va:416)
at org.apache.tomcat.util.Thr eadPool$Co ntrolRunna ble.run(Th readPool.j ava:501)
at java.lang.Thread.run(Threa d.java:484 )
.. So am I missing something ?
Cheers
Here it is:
14-Sep-01 15:41:33: javax.mail.NoSuchProviderE
14-Sep-01 15:41:33: Stack trace: javax.mail.NoSuchProviderE
at javax.mail.Session.getTran
at javax.mail.Transport.send0
at javax.mail.Transport.send(
at com.bluebase.mail.MailBean
at com.bluewave.mayflower.req
at com.bluebase.model2.Reques
at _0002fcontroller_0002ejspc
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.tomcat.core.Ser
at org.apache.tomcat.core.Han
at org.apache.tomcat.core.Ser
at org.apache.tomcat.core.Con
at org.apache.tomcat.core.Con
at org.apache.tomcat.service.
at org.apache.tomcat.service.
at org.apache.tomcat.util.Thr
at java.lang.Thread.run(Threa
.. So am I missing something ?
Cheers
Look at your property setting for the smtp host. Is it correct? Also, are you trying to test the program from a networked computer, or a laptop?
properties.put("smtp2.comp anyname.co m", "smtp");
I think it should be:
properties.put("mail.smtp. host", "smtp2.companyname.com");
(key, value) pairs - u see...
Then:
Transport.send(mimemessage , ainternetaddress);
Try to change it to:
session1.getTransport("smt p").send(m essage);
I think it should be:
properties.put("mail.smtp.
(key, value) pairs - u see...
Then:
Transport.send(mimemessage
Try to change it to:
session1.getTransport("smt
ASKER
i am doing this..
Properties props = new Properties();
props.put("mail.smtp.host" ,getProper ty(pageCon text,"prop s","smtp") );
remember this works on my computer (workstation - NT on network) but on another dev server it is not !
I have logged the IP of the SMTP server, it is correct.. also remember the whole thing works on my machine !
Also, I wrote a perlscript to send an email from the dev box which did work, so the SMTP server is visible to the new server on which my Java Mail is not running.
I will try the session1 thing now.
Cheers
M
Properties props = new Properties();
props.put("mail.smtp.host"
remember this works on my computer (workstation - NT on network) but on another dev server it is not !
I have logged the IP of the SMTP server, it is correct.. also remember the whole thing works on my machine !
Also, I wrote a perlscript to send an email from the dev box which did work, so the SMTP server is visible to the new server on which my Java Mail is not running.
I will try the session1 thing now.
Cheers
M
ASKER
Does "javax.mail.NoSuchProvider Exception" message mean that the SMTP server can not be found ?
No. It rather means that there's not an implementator for the protocol
"The Provider is a class that describes a protocol implementation. The values come from the javamail.providers & javamail.default.providers resource files."
That's why I told you to specify the protocol:
session1.getTransport("smt p").send(m essage);
"The Provider is a class that describes a protocol implementation. The values come from the javamail.providers & javamail.default.providers
That's why I told you to specify the protocol:
session1.getTransport("smt
Most probably you've had the proper default values at the fisrt machine and dafault value of rfc822 at the another.
So, rfc822 is the type of the InternetAddress.
Session is trying to retrieve the proper provider for it by serarching in the META-INF/javamail.address. map and META-INF/javamail.default. address.ma p files in the mail.jar
Session is trying to retrieve the proper provider for it by serarching in the META-INF/javamail.address.
This is what could be read in my javamail.default.address.m ap rfc822=smtp
See, this is to mean: if the address type is InternetAddress, use the smtp protocol.
I don't know why this property could not be read using your installation - would u check whether these files are present in your mail.jar?
Anyway, I think that expleicitly specifying the protocol would do the work...
See, this is to mean: if the address type is InternetAddress, use the smtp protocol.
I don't know why this property could not be read using your installation - would u check whether these files are present in your mail.jar?
Anyway, I think that expleicitly specifying the protocol would do the work...
ASKER
Does "javax.mail.NoSuchProvider Exception" message mean that the SMTP server can not be found ?
Nooo, it means no one has implemented the corresponding protocol and the mail api can't use it.
"Provider" in this case means "provider of implementation".
It's not the server that's missing it's the protocol...
"Provider" in this case means "provider of implementation".
It's not the server that's missing it's the protocol...
To be more exact, it's the support of the protocol.
See, how all the stuff works:
1. You call Transport.sendMessage(mess , adrr[])
2. For every address Session.getTransport(addr) is called
3. Depending on the type of the address, the rellevant protocol's provider is searched in the files that I wrote you about.
4. Since your files are either missing or corrupted, the protocol to be used cannot be specfied properly hence appropriate implementation is not found => the NoSuchProviderException for Address type...
See, how all the stuff works:
1. You call Transport.sendMessage(mess
2. For every address Session.getTransport(addr)
3. Depending on the type of the address, the rellevant protocol's provider is searched in the files that I wrote you about.
4. Since your files are either missing or corrupted, the protocol to be used cannot be specfied properly hence appropriate implementation is not found => the NoSuchProviderException for Address type...
Would u please go and have a look at your mail.jar? There should be files like META-INF/javamail.address. map
and META-INF/javamail.default. address.ma p.
Or - at least - one of them...
Would u tell me what's in?
BTW, have u tried using
session.getTransport("smtp ").sendMes sage(messa ge)?
and META-INF/javamail.default.
Or - at least - one of them...
Would u tell me what's in?
BTW, have u tried using
session.getTransport("smtp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i tried..
s.getTransport("smtp").sen d(msg);
Again, works on my machine.. not on the other dev box.
I have the default WEB-INF
...
WHOOOOO.. fixed it.. i didnt unpack the mail.jsp..
s.getTransport("smtp").sen
Again, works on my machine.. not on the other dev box.
I have the default WEB-INF
...
WHOOOOO.. fixed it.. i didnt unpack the mail.jsp..
ASKER
I am silly !
laa laa 1 will confirm this :)
peace.
laa laa 1 will confirm this :)
peace.
Hey Mehdi, how about some more points for la la? He deserves > 1 point per response! :-)
Dorothy
Dorothy
Thanks, Dorothy :)
In fact "she" is who deserves :)
One can do miracles with the old Jad :)
Good luck, all!
In fact "she" is who deserves :)
One can do miracles with the old Jad :)
Good luck, all!
Thanks to you, Mehdi, too :)
ASKER