Solved

SMTP in Java

Posted on 2002-06-22
10
1,427 Views
Last Modified: 2007-11-27
I am creating a small program that I can use to send emails to people.  I have gotten the program to work perfectly when I use SMTP servers that dont require authentication.  My question is this:  How would I modify my code to work for a SMTP server that required authentication?

Code snippet:

Socket s = new Socket(smtp.getText(),25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader
    (s.getInputStream()));
               
send(null);
send("HELO " + InetAddress.getLocalHost().getHostName());
send("MAIL FROM: " + from.getText());
send("RCPT TO: " + to.getText());
send("DATA");
out.println("Subject: " + subject.getText());
out.println(_body.getText());
send(".");
s.close();

So what would I add to get this to work with Authentication?
0
Comment
Question by:lwinkenb
[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
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7100432
I must say I've never tried an SMTP server requiring authentication, but why not try the same approach as POP and see what happens:
send("USER" + user.getText());
send("PASS" + pass.getText());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7100848
You'll need to look at RFC 2554 for this:
http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc2554.html
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7101420
I looked at the link, but it was pretty confusing to me.  This is what I tried:

C: HELO
C: EHLO
C: MAIL FROM: <address>
C: AUTH LOGIN
S: VXNlcm5hbWU6
C: <What in the heck do I put here?>
S: UGFzc3dvcmQ6
C: <I probably need to put something here>
S: 5.7.3 Authentication unsuccessful. (this is what I get)

What am I doing wrong?  The whole point of this is to be able to send messages to a domain outside my SMTP domain.  If I'm way off track with the above, please let me know :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 7101456
This is new to me as well, but my reading from the example in the RFC of the first part of the exchange is as follows:

0: S: Server welcome
1. C: EHLO <your domain name>
2. S: Read server response. One of the lines should contain the authorisation methods it supports.
3. C: AUTH <your choice from the list of server-supported types of authentication>
4. S: 334 <base 64 encoded string>

It's ironic that base 64 is coming into this at all, but I'd love to know what sort of thing is contained in 4.
Give it a try and let me know, then perhaps we can work out the next bit :-)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7101908
Here we go:

S: 220 ubax.server.ubax.net Microsoft ESMTP MAIL Service, Version: 5.0.2195.4453 ready at  Sun, 23 Jun 2002 10:37:12 -0700

C: EHLO luke

S: 250-ubax.server.ubax.net Hello [192.168.1.8]
250-TURN
250-ATRN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK

C: AUTH LOGIN

S: 334 VXNlcm5hbWU6

C: (I dont know what to put here, but if I enter in anything, the server responds with...)

S: 334 UGFzc3dvcmQ6

C: (Again I dont know what to put in, so the server responds with...)

S: 535 5.7.3 Authentication unsuccessful.

If I try one of the other supported types of authentication, the server will respond with:
GSSAPI supported.
or
NTLM supported.

AUTH LOGIN is the only one that returns with an encoded string.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7103239
Oh dear - it's unfortunate it' Microsoft! I wouldn't be surprised if they merged this authenticating ESMTP with their own proprietary extensions. For instance - what's NTLM? I'll look at the next bit. What do the base 64 server strings say if you decode them?
0
 

Expert Comment

by:chintamanidhotre
ID: 7103242
Why dont u use the Java Mail API.It is very easy to send mails using this API's.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7103283
I'm a bit busy at the moment, but the following should help:

My suspicions were correct - NTLM is proprietary although according to this doc it may be reasonably straightforward: http://www.innovation.ch/java/ntlm.html.
Note, however, that the doc is based on the result of reverse engineering.

As for GSSAPI, it looks like this is a wrapper arouind Kerberos: http://docs.sun.com/?q=GSSAPI&p=/doc/806-3814
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 7103892
you can use base64 authentication:
Socket s = new Socket(smtp.getText(),25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader
   (s.getInputStream()));
             
send(null);
new sun.misc.BASE64Encoder enc = new new sun.misc.BASE64Encoder();
send("HELO " + InetAddress.getLocalHost().getHostName());
// S: 250 OK
send("AUTH LOGIN");
// 334 VXNlcm5hbWU6
send(enc.encode(username.getBytes()));
// 334 UGFzc3dvcmQ6
send(enc.encode(password.getBytes()));
// 235 LOGIN authentication successful
send("MAIL FROM: " + from.getText());
send("RCPT TO: " + to.getText());
send("DATA");
out.println("Subject: " + subject.getText());
out.println(_body.getText());
send(".");
s.close();

0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7104933
Exactly what I needed.  I just needed to convert the username/password to base64 before transmitting them.  

Thanks!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 116
going to wrong jsp page 2 70
SequenceInputStream example 3 38
How to access inner class in java (android)?? 13 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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