Sending mail using sockets
843830Apr 21 2004 — edited Apr 21 2004Can anyone help?
I am quite new to Java but have copied some code from a book which sends smtp mail using a socket connection. The code works fine from a unix environment but when I run the same on Windows NT client (same network, same smtp mail server) the mail does not arrive despite no indication of problems in the code output.
I have read that there are issues with line termination between windows and unix so have amended the code to add "\r\n" to each communication but to no avail.
Any ideas ??
import java.io.*;
import java.net.*;
public class SMTPDemo {
public static void main(String args[])
throws IOException, UnknownHostException {
String msgFile;
String from, to, mailHost;
if (args.length !=4) {
System.out.println("Usage: java SMTPDemo msgFile from to mailHost");
System.exit(10);
}
msgFile = args[0];
from = args[1];
to = args[2];
mailHost = args[3];
checkEmailAddress (from);
checkEmailAddress (to);
SMTP mail = new SMTP (mailHost);
if (mail != null) {
if (mail.send (new FileReader (msgFile), from, to) ) {
System.out.println("Mail sent.");
}
else {
System.out.println("Connect to SMTP server failed!");
}
}
System.out.println("Done.");
}
static void checkEmailAddress (String address) {
if (address.indexOf('@') == -1) {
System.out.println("Invalid e-mail address '" + address + "'");
System.exit (10);
}
}
}
class SMTP {
public final static int SMTP_PORT = 25;
InetAddress mailHost;
InetAddress ourselves;
BufferedReader in;
PrintWriter out;
public SMTP (String host) throws UnknownHostException {
mailHost = InetAddress.getByName (host);
ourselves = InetAddress.getLocalHost();
System.out.println("mailhost = " + mailHost);
System.out.println("localhost = " + ourselves);
System.out.println("SMTP constructor done\n");
}
public boolean send (FileReader msgg, String from, String to)
throws IOException {
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader msg;
msg = new BufferedReader (msgg);
smtpPipe = new Socket (mailHost, SMTP_PORT);
if (smtpPipe == null) {
return false;
}
// get raw streams
inn = smtpPipe.getInputStream();
outt = smtpPipe.getOutputStream();
// turn into usable ones
in = new BufferedReader (new InputStreamReader(inn));
out = new PrintWriter (new OutputStreamWriter(outt), true);
if (inn==null || outt==null) {
System.out.println("Failed to open streams to socket.");
return false;
}
String initialID = in.readLine();
System.out.println(initialID);
System.out.println("HELO " + ourselves.getHostName());
out.println("HELO " + ourselves.getHostName() + "\r\n");
String welcome = in.readLine();
System.out.println(welcome);
System.out.println("MAIL From:<" + from + ">");
out.println("MAIL From:<" + from + ">" + "\r\n");
String senderOK = in.readLine();
System.out.println(senderOK);
System.out.println("RCPT TO:<" + to + ">");
out.println("RCPT TO:<" + to + ">" + "\r\n");
String recipientOK = in.readLine();
System.out.println(recipientOK);
System.out.println("DATA");
out.println("DATA" + "\r\n");
String line;
while ((line = msg.readLine()) != null) {
System.out.println(line);
out.println(line + "\r\n");
}
System.out.println(".");
out.println("." + "\r\n");
String acceptedOK = in.readLine();
System.out.println(acceptedOK);
System.out.println("QUIT");
out.println("QUIT" + "\r\n");
return true;
}
}