Solved

Sending Email with Java

Posted on 2001-09-13
25
965 Views
Last Modified: 2013-11-24
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
Comment
Question by:mehdi
  • 12
  • 7
  • 3
  • +2
25 Comments
 
LVL 1

Author Comment

by:mehdi
Comment Utility
sorry i lied.. the mailbean was not written by me..
0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
check the log files .. your bean probably can't find the SMTP server or there is some other error
0
 
LVL 1

Expert Comment

by:kanthonym
Comment Utility

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
 
LVL 6

Expert Comment

by:dorothy2
Comment Utility
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 1

Expert Comment

by:kanthonym
Comment Utility
Agreed, send your code out here for review.
0
 
LVL 1

Author Comment

by:mehdi
Comment Utility
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
 
LVL 6

Expert Comment

by:dorothy2
Comment Utility
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 1

Author Comment

by:mehdi
Comment Utility
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
 
LVL 1

Author Comment

by:mehdi
Comment Utility
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
Most probably you've had the proper default values at the fisrt machine and dafault value of rfc822 at the another.
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 1

Author Comment

by:mehdi
Comment Utility
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
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
 
LVL 2

Accepted Solution

by:
ia_ia_ia_1 earned 100 total points
Comment Utility
Hey, what's up?
I gotta go and fetch something for supper.
I'll check your progress in an hour or so...
0
 
LVL 1

Author Comment

by:mehdi
Comment Utility
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
 
LVL 1

Author Comment

by:mehdi
Comment Utility
I am silly !
laa laa 1 will confirm this :)

peace.
0
 
LVL 6

Expert Comment

by:dorothy2
Comment Utility
Hey Mehdi, how about some more points for la la? He deserves > 1 point per response! :-)

Dorothy
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
Thanks, Dorothy :)
In fact "she" is who deserves :)

One can do miracles with the old Jad :)

Good luck, all!
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
Comment Utility
Thanks to you, Mehdi, too :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now