This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sending Email from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending Email from Java" Watch "Sending Email from Java" New topic
Author

Sending Email from Java

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I have asked this question earlier in jboss section and also on jboss website. But I was wrong in posting my query. The solution that I got was jBoss specific. But my requirement needs me to send the mail without using the jBoss mail service. So I'm posting this again. I want to send the mail using simple java mail API. I can't use jBoss services. This is the code that I'm using



Now the problem is that when the tr.connect method is called, I get a messaging connection. I am connecting to localhost as you can see. I was told to use JES for sending the mail. I also found that the username and password is not needed while connecting when using JES. But I'm not able to make this work. I changed the code to use the mail server of my company. I used the url of the mail server and the username and password of the mail server. But still I'm getting Messaging Exception. There is nothing wrong with the code as it is working on the live site on which it is actually deployed. So basically I just need to configure JES or any other mail server properly. Can anyone give me an idea or a web resource to make this work.

Also can I use a server like gmail to send the mail using my username and password of gmail. I know this may sound stupid if I'm wrong ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I am providing some more details if that might help you understand the problem. The exact exception looks like this on jBoss console



I have a local copy of JES running and I used netstat -abno command to see if JES is listening on port 25. This is the output to prove that port 25 i.e. SMTP port is running

TCP 0.0.0.0:25
[java.exe]

So I can't understand what the actual problem is. Any help is welcome...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Ok I'm one step closer to the solution. My antivirus was blocking port 25 to prevent worms from doing mass mails. Actually someone told me to try to connect to 127.0.0.1 25 through telnet to check if the port is blocked. So I tried telnet and couldn't connect. So then I checked my antivirus and found the problem there. But now I'm facing another problem. I am using JES to send the mails. But my JES is giving this error while sending emails



Can someone help me with this...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

Well the SMTP response codes seem to indicate that there's something wrong with the receiver's mailbox, are you sure it exists?

450 - Requested mail action not taken: mailbox unavailable
553 - Requested action not taken: mailbox name not allowed

These are repsonse codes to the SMTP RCPT command.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well thanks Jelle for the information. I think there was a problem because of configuration of JES. In the mail.conf file there was an entry of allowed outgoing from addresses which was not configured. This is the part of the mail.conf file



So I added my mail address to the allowed list. Not there is no error code with the error but still the error is there. This is the new error from JES now



Now there are a few things to clarify. I have changed the email id but the actual email ID that I'm using is valid. But the from and to fields have the same email id if that makes a difference. Also I'm having some internet connection problem today so it might be due to that. I can send mails and chat using normal gmail but JES might not be able to handle the network problem. But I'm not sure what is the cause of this problem...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well I couldn't configure JES on my local machine so I used the email server of my company. Still the code was not working. I was getting this exception

Sending message failed: javax.mail.MessagingException: 554 Sending address not accepted due to spam filter

I was not able to find out the reason for the error. The same sending address was working on a test program that I created and which I ran from the command line. So I had to change the mail.jar in the jboss 4.0.4. I replaced the file with the new mail.jar that I downloaded from internet. And boom, it started working. I wrote this just for anyone who faces a similar problem. I know that there can't be a problem with jBoss but I think it could be because of the version of JDK on my system or because of a similar problem...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
Sending message failed: javax.mail.MessagingException: 554 Sending address not accepted due to spam filter

What does the sysadmin say about this? He should be in a position to check why this happens, and do something about it if warranted.


Ping & DNS - my free Android networking tools app
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I don't know the reason but I surely know that the problem was solved when I replaced the mail.jar library file as I told earlier too...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending Email from Java
 
Similar Threads
JavaMail EOf on socket
Sending email from java. Pls Help!!!
Email from jBoss
Problem with mail api on google app engine.
How can I receive failure notice using Java?