This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to talk with a mail server in Java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How do we execute and read responses from Java?

For example:




Open a connection to <server domain> on port 25

Send message: HELO
--- read response

Send message: MAIL FROM:<mail@domain1.com>
--- read response

Send message: RCPT TO:<mail@domain2.com>
--- read response



I've tried using sockets and executing telnet through cmd /C, but in either case I haven't been able to get a response.  

Using the command prompt with telnet would look like this:

telnet server 25
HELO
MAIL FROM:<mail@domain.com>
RCPT TO:<mail@domain2.com>


 
Bartender
Posts: 1164
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, welcome to the ranch!
Have a look at Java Mail API, it allows you to do pretty anything with a mail server. As far as I remember,  JavaMail API covers smtp,pop3, and IMAP protocols hiding low level details with a bunch of classes (despite the fact that, as many Java APIs, it looks a bit over engineered).
 
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want your Java application to be the server side or the client side of the SMTP communications?
 
Sori Lao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Do you want your Java application to be the server side or the client side of the SMTP communications?



I'd just like to start by figuring out the client side, but before I send any email I first want to verify through TCP as mentioned in this comment I found on Stack Exchange:  

You can use JavaMail API and try to send an email.

If you don't want to actually send the email, you can open a TCP socket to port number 25 of your mail server and send the following commands:

HELO yourdomain.com
MAIL FROM: youremail@yourdomain.com
RCPT TO: recipient@recipientdomain.com

Make sure you check all server responses after each command issued. If you don't see any errors until that point, then probably you will be able to send emails with that SMTP server.

 
Ron McLeod
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sori Lao wrote:I'd just like to start by figuring out the client side, but before I send any email I first want to verify through TCP as mentioned in this comment I found on Stack Exchange ...


Which email server are you testing with?  Do you have your own captive server, or are you trying testing against something like Gmail?
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sori Lao wrote:I'd just like to start by figuring out the client side, but before I send any email I first want to verify through TCP as mentioned in this comment I found on Stack Exchange:  



What's the point of that? Seems to me that the way to test your e-mail code is to try sending yourself an e-mail. I don't see the point of writing some code which isn't going to be part of your application just to test that you're connecting to an SMTP server.
 
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, telneting into port 25 to send a mail will only work on the same network, and possibly not even then. If this is a remote server you would need to authenticate yourself, which is not quite so easy to accomplish on the command line.
 
Sori Lao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Sori Lao wrote:I'd just like to start by figuring out the client side, but before I send any email I first want to verify through TCP as mentioned in this comment I found on Stack Exchange ...


Which email server are you testing with?  Do you have your own captive server, or are you trying testing against something like Gmail?



I have a mail server but was testing against gmail as it seems more practical.

telneting into port 25 to send a mail will only work on the same network, and possibly not even then.



Yeah, I noticed a lot of documents talking about how to do so are quite outdated.
 
Ranch Hand
Posts: 186
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Also, telneting into port 25 to send a mail will only work on the same network, and possibly not even then. If this is a remote server you would need to authenticate yourself, which is not quite so easy to accomplish on the command line.


Well, shortly, this is only true for submission as registered user of the server you want to use. Also, mail submission is done over TCP/465 or TCP/587 as TCP/25 is for inter-server communication required for mail to be delievered from one server to another.

I hosting my own mail server wich requires a lot of maintenance like keep blacklist records clean, set up DNS correctly, and some other stuff. Matching all these I can drop a mail to any server by just connect to it and issue required commands to do so.
I could explain it in way more exhaustive manner, but I try to keep it simple for this post as a "complete", or better: "recommended" guide how to setup your own mail server could easily fill a few posts with max limit of 10k characters.

back to topic:

@TO
Everything like "using CMD to execute system commands" is sort of - well, WRONG! I don't mean you can't do it, but rather there're way better ways to do it, or not possible at all.
It depends on what is the goal you want to accomplish. If you want send mails through your mail server - JavaMail lib is the best way to go. Don't start to bother with low-level SMTP, neither over telnet or Socket. If you want to write your own mail server - good luck, but possible a very huge and longterm project for a one-man-show, aside from obvious lack of knowledge about internet mail.
Also: your EHLO command is incomplete. EHLO (or basic HELO) requires an identifier as paramter like your server name. For example: "HELO cryptearth.de". This could be the reason why you not getting any reply, or most likely error messages, as your EHLO command is incomplete and the server maybe wait for it to complete (although this would be not following SMTP RFC as it states that EHLO <identifier> has to be on the same line followed by CRLF. Anything else a server compliant with SMTP should return an error. Also, as telnet is a program itself you don't need to call it with CMD but could do so directly. CMD would only act as kind of wrapper in this way. As you say you don't get any reply it seems like you don't read the process output correctly.
To best help you show us your code.

For reference, here's an example how to use JavaMail to submit a mail to your mail server (all in main() and Exception-handling bypassed with "throws Exception"):
note - code is written in browser without compile test - may contain minor errors to be fixed - just an example


If you want to use TCP/465 over TLS - PLEASE - don't do crap like setting mail.smtp.SocketFactory to javax.net.ssl.SSLSocketFactory - this is the most wrong way - SMTP over TLS on TCP/465 is called SMTPS - and should use this way:


This is the way you mostly find it on the net:

NO - this is just WRONG and not how JavaMail is spupposed to be used - I don't even know why this is even possible and should be removed ASAP - but what ever. It could just be "mis-used" to try to drop a mail directly at a target server as it lacks SMTP AUTH (wich, as noted in doc, mail.smtp.auth is REQUIRED to set to true - default is false wich will no do any auth at all) and mis-use the wrong protocol SMTP over socket-level TLS (wich is SMTPS) and override socketfactory to SSLSocketFactory - wich isn't needed when correctly using SMTPS.
 
Police line, do not cross. Well, this tiny ad can go through:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!