Link to home
Start Free TrialLog in
Avatar of jjackson2004
jjackson2004Flag for United States of America

asked on

Error in java code

Still having rookie issues with creating a java package from scratch.  I have the following classes, MailClient, Message, Envelope, SMTPConnection.

I am assuming that MailClient should be the main class and used to invoke all of this instead of having a MailAgent class that would call MailClient.

Anyway, I have created the package in NetBeans, but when I clean and build, I receive an error for 'envelope' in MailClient and I cannot figure out why.

Please take a look at if and see if you can assist.  Thanks


Envelope.java
MailClient.java
Message.java
SMTPConnection.java
Avatar of jjackson2004
jjackson2004
Flag of United States of America image

ASKER

the error is that it cannot find the symbol 'envelope'
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
ASKER CERTIFIED SOLUTION
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
No - you don't need two try/catch blocks - just one
One or two is really not that important - it actually even excutes and shows gui, though I didn't try to ssend a message.

What is really very important is never do like this:

 try {
		envelope = new Envelope(mailMessage,
						 serverField.getText());
	    } catch (UnknownHostException e) {
		/* If there is an error, do not go further */
		return;
	    }

Open in new window



Here you catch exception but does not print anyting in the catch block
This is teribl practicve - even in the quickets and dirtiest code
that will give you so much pain - never eever do that!

Best thing in every catch loop to put  ex.printStackTrace()




Eeven though you have return;
but it may be difficult to understand that you have exception
So always put ex.printStacktrace() inside catch loop - it will save you a lot of time, believe me I made this mistake many times in the beginning
Avoid also using return statements in error handling. This makes the code difficult to maintain and somewhat negates the point of exceptions, which have their own flow control.

You should also really log errors in order to determine what is happening in your code
Your code in the area in question should look more like the following, although in place of stack trace printing should really be error logging AND stack trace logging
try {
                Envelope envelope = new Envelope(mailMessage, serverField.getText());
                connection = new SMTPConnection(envelope);
                connection.send(envelope);
            } catch (UnknownHostException e) {
                /* If there is an error, do not go further */
		e.printStackTrace();	
            }
            catch (IOException e) {
                e.printStackTrace();	
            }
	    finally {
                try { connection.close(); } catch(Exception e) { /* ignore */ }	
	    }

            System.out.println("Mail sent succesfully!");

Open in new window

printStackTrace() will print for you the exact line where the error occured so in case of longer blocks
iit would be convenient to poinpoint the problem,
but much more importantly, it will give you long printourt of exceptiion trace and would not allow you
to leave the error unnoticed - and this is where you'll spend most of your debugging time if you forget to
make some explicit printout inside the block.

Compiler sometimes requires you to put the try/catch loop and if you are starting programming in java
you tend to do it quickly just to satsify compiler and leave
that place inside the loop empty. Then you forget about it and later on there is an error which is being caught and disappears into
thin air - and to find such error is sometimes almost impossible.
So even if you are writing code while jumping with parashute (perhpas still not the best practice),
still take additional second and add printStackTrace() within the loop.
you guys are awesome.  I thought it might be that envelope was in the try/catch but I am still not sure of myself on these things.

Now, would somebody mind looking into why the send button does not seem to do anything when I am testing it?

Thanks

I am afraid it will send something somewhere ?
Guaranteed, that it would not?
what are those hosts doing:
ultra.allegheny.edu
adsl.meadville-1
it asks for local smtp server but is it using it or is it rather going to these serevrs above?
never mind,  it is trying in the background.

Changed that server listed in the code to gsu-edu.mail.eo.outlook.com

getting error:  "Sending failed:  java.net.ConnectException:  Connection timed out:  connect"

guess I chose the wrong mail server.  Working on trying to figure out nslookup to do a MX query.
So it does not use the local smtp server which I'd provide in the text box - true?
So I should explicitly put my loacl serevr thhere to try?

And why there are twio of them?
um, these would seem like my questions to you.  Two of them what?
You amy want to look at this trail - it has solution some information on how to use Gmail
and Hotmail as public smtp servers
https://www.experts-exchange.com/questions/27326341/Using-JavaMail-to-send-email-with-Live-Hotmail-SMTP-server.html

I mean you mention two servers explicitly in your code:
ultra.allegheny.edu
adsl.meadville-1

besides you require to enter local SMTP server

how are those three servers playing ?

In order to send email you normally need to connect just to one SMTP server

So why do you have three in your code?


my mistake, reusing some old code.  I believe that now I have corrected them.
post your new smtp connection java file - I'll replace with my local smtp serevr and try if it works for me
how would I check to see what my local mail server.  I use Uverse.  should I nsloopup att.net?

Do you know how to verify the sender's address?  Something about javas System class has something in it for that I am told.
SMTPConnection.java
think I found the answer to my local mail server.  it would appear to be one of three

scc-mailrelay.att.net
aln-mailrelay.att.net
frf.mailrelay.att.net
may be reasonable.
worked nicely for me with my smtp server
though I have different localhost and smtp server - maybe you happen to have the same -
don'tknow why you had them identical.
I provided our smtp serevre for the server and IP of my local machine for localhost and got nice email
I have added a Cc: field.  Would you mind testing it for me.  I still have issues with the local mailserver on my system.

Thanks again.  this is very informative and extremely helpful.
Message.java
MailClient.java
Look at this - if you can go away form sockets and do it through higher level with JavaMail, you can try this
using Gmail SMTP server (you'll of course need gmail account and hat your company allows you to comnnect):
see here:
http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
Yes, works fine for me this variant; and sends CC also
I try not to do anything involving google except searches that I don't mind them keeping forever.  But I will look into it in my endeavor to try to learn as much as I can about different subjects.

When this is over, if you have any suggestions of how to remove some of the hidden google stuff on my computer i would greatly appreciate.  Or I can submit a new question.  Yeah, I know, conspiracy theory.

Do you know how to verify the sender address using System class and the InetAddress class for finding the name of the local host?
No don't knwo about use of System class in this respect

This is something about sender verification:
http://www.rgagnon.com/javadetails/java-0452.html
I think you could ask your IT guys about your smtp server

we have it named like that:

mailhub.company_name.com

check maybe you have such server either
thank you again so much.  I will have only one more thing to ask of you.  I am making one more change to the program.  Would you mind testing it one last time?

Thanks
sure, let me know what is the change and post the file
Added Message-ID to the headers (supposedly according to RFC 822)


Message.java
MailClient.java
maybe I am supposed to have the messageid show up on the email client where the other fields are shown?  hard to read the RFC.
works OK. Don't know what you mean about messageId but sends fine
A unique message id I generated with uuid.  I take it does not show up in your email client.
No, but it showed in the protocol of email sending
I would strongly urge you to use the JavaMail api instead of trying to reinvent it. You'll get a lot more functionality and it will be a lot less error-prone
great responses.  Thanks!