Link to home
Start Free TrialLog in
Avatar of mehdi
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 :)
Avatar of mehdi
mehdi

ASKER

sorry i lied.. the mailbean was not written by me..
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||subject==null||msg==null) {
//java.lang.System.out.println("At least one parameter is missing.");
               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.companyname.com", "smtp");
             Session session1 = Session.getDefaultInstance(properties, null);
             MimeMessage mimemessage = new MimeMessage(session1);
             try
             {
                 InternetAddress ainternetaddress[] = null;
                 ainternetaddress = InternetAddress.parse(email_to_addresses, false);
                 mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, ainternetaddress);
                 mimemessage.setFrom(new InternetAddress(from));
                 mimemessage.setSubject(subject);
                 mimemessage.setContent(msg, "text/plain");
                 Transport.send(mimemessage, ainternetaddress);
             }
             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
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?
Agreed, send your code out here for review.
Avatar of mehdi

ASKER

Ok folks, i appreciate the feedback.. I got the exception :)

Here it is:

14-Sep-01 15:41:33: javax.mail.NoSuchProviderException: No provider for Address type: rfc822
14-Sep-01 15:41:33: Stack trace: javax.mail.NoSuchProviderException: No provider for Address type: rfc822
        at javax.mail.Session.getTransport(Session.java:475)
        at javax.mail.Transport.send0(Transport.java:154)
        at javax.mail.Transport.send(Transport.java:80)
        at com.bluebase.mail.MailBean.emailProcess(MailBean.java:58)
        at com.bluewave.mayflower.requesthandlers.MFLMailFormUser.handleRequest(MFLMailFormUser.java:73)
        at com.bluebase.model2.RequestController.getNextPage(RequestController.java:74)
        at _0002fcontroller_0002ejspcontroller_jsp_0._jspService(_0002fcontroller_0002ejspcontroller_jsp_0.java:132)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:282)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
        at org.apache.tomcat.core.Handler.service(Handler.java:287)
        at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
        at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
        at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
        at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection(Ajp12ConnectionHandler.java:166)
        at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
        at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
        at java.lang.Thread.run(Thread.java:484)


.. 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.companyname.com", "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("smtp").send(message);
           
Avatar of mehdi

ASKER

i am doing this..

        Properties props = new Properties();
        props.put("mail.smtp.host",getProperty(pageContext,"props","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
Avatar of mehdi

ASKER

Does "javax.mail.NoSuchProviderException" 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("smtp").send(message);
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.map files in the mail.jar
This is what could be read in my javamail.default.address.map 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...
Avatar of mehdi

ASKER

Does "javax.mail.NoSuchProviderException" 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...
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...
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.map.
Or - at least - one of them...

Would u tell me what's in?

BTW, have u tried using

session.getTransport("smtp").sendMessage(message)?
ASKER CERTIFIED SOLUTION
Avatar of ia_ia_ia_1
ia_ia_ia_1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mehdi

ASKER

i tried..

s.getTransport("smtp").send(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..
Avatar of mehdi

ASKER

I am silly !
laa laa 1 will confirm this :)

peace.
Hey Mehdi, how about some more points for la la? He deserves > 1 point per response! :-)

Dorothy
Thanks, Dorothy :)
In fact "she" is who deserves :)

One can do miracles with the old Jad :)

Good luck, all!
Thanks to you, Mehdi, too :)