Solved

Sending Email with Java

Posted on 2001-09-13
25
1,003 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 7
  • 3
  • +2
25 Comments
 
LVL 1

Author Comment

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

Expert Comment

by:heyhey_
ID: 6479932
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
ID: 6479981

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 6

Expert Comment

by:dorothy2
ID: 6480689
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
ID: 6481008
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
ID: 6481013
Agreed, send your code out here for review.
0
 
LVL 1

Author Comment

by:mehdi
ID: 6482823
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
ID: 6482861
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
ID: 6482876
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
ID: 6482912
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
ID: 6482915
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6482943
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
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6482952
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
ID: 6483053
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
ID: 6483065
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
ID: 6483096
Does "javax.mail.NoSuchProviderException" message mean that the SMTP server can not be found ?
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6483110
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
ID: 6483126
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
ID: 6483132
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
ID: 6483214
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
ID: 6483252
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
ID: 6483258
I am silly !
laa laa 1 will confirm this :)

peace.
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 6483394
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
ID: 6483473
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
ID: 6483486
Thanks to you, Mehdi, too :)
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

635 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