• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1009
  • Last Modified:

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 :)
0
mehdi
Asked:
mehdi
  • 12
  • 7
  • 3
  • +2
1 Solution
 
mehdiAuthor Commented:
sorry i lied.. the mailbean was not written by me..
0
 
heyhey_Commented:
check the log files .. your bean probably can't find the SMTP server or there is some other error
0
 
kanthonymCommented:

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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
dorothy2Commented:
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
0
 
ia_ia_ia_1Commented:
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?
0
 
kanthonymCommented:
Agreed, send your code out here for review.
0
 
mehdiAuthor Commented:
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
0
 
dorothy2Commented:
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?
0
 
ia_ia_ia_1Commented:
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);
           
0
 
mehdiAuthor Commented:
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
0
 
mehdiAuthor Commented:
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
ia_ia_ia_1Commented:
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);
0
 
ia_ia_ia_1Commented:
Most probably you've had the proper default values at the fisrt machine and dafault value of rfc822 at the another.
0
 
ia_ia_ia_1Commented:
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
0
 
ia_ia_ia_1Commented:
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...
0
 
mehdiAuthor Commented:
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
ia_ia_ia_1Commented:
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...
0
 
ia_ia_ia_1Commented:
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...
0
 
ia_ia_ia_1Commented:
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)?
0
 
ia_ia_ia_1Commented:
Hey, what's up?
I gotta go and fetch something for supper.
I'll check your progress in an hour or so...
0
 
mehdiAuthor Commented:
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..
0
 
mehdiAuthor Commented:
I am silly !
laa laa 1 will confirm this :)

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

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

One can do miracles with the old Jad :)

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 12
  • 7
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now