Import java.net.*;
import java.io.*;
/**
* <p>title: Send mail using SMTP </p>
* <p>description: This example sends mail based on the SMTP protocol </p>
by using the socket method
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:sendSMTPMail.java</p>
* @version 1.0
*/
public class Sendsmtpmail {
/**
*<br> Method Description: Main method
*<br> Input Parameters: 1. Server ip;2. Email address
*<br> return type:
*/
public static void Main (string[] arges) {
if (arges.length!=2) {
System.out.println ("Use Java sendsmtpmail hostname | mail to ");
return;
}
Sendsmtpmail t = new Sendsmtpmail ();
T.sendmail (Arges[0], arges[1]);
}
/**
*<br> Method Description: Send mail
*<br> input parameter: String mailserver mail receiving server
*<br> input parameter: String recipient receive mail address
*<br> return type:
*/
public void SendMail (string mailserver, String recipient) {
try {
//With socket Open 25 port
socket s = new socket (mailserver, 25);
//cache input and output
BufferedReader in = new BufferedReader
(New InputStreamReader (S.getinputstream (), "8859_1"));
BufferedWriter out = new BufferedWriter
(New OutputStreamWriter (S.getoutputstream (), "8859_1"));
//issue "HELO" command to express greetings to the server
send (in, out, "HELO theworld");
//Tell the server my email address, some servers have to check this address
send (in, out, "MAIL from: <>");
//Use the "RCPT to" command to tell the server to explain the e-mail address of the message
send (in, out, "RCPT to:" + recipient);
Sending a "DATA" means that the following will be the message body
send (in, out, "DATA");
//Use subject command to annotate message subject
Send (out, "Subject: This is a test program!") ");
//Use "from" to mark the source of the message
Send (out, "From:riverwind <>");
Send (out, "\ n");
//Message body
Send (out, "This is a message sent using the SMTP protocol!") If disturb please delete! ");
Send (out, "\n.\n");
//Send "QUIT" Port Mail
send (in, out, "QUIT");
S.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
/**
*<br> Method Description: Send a message and receive a reply
*<br> input Parameters:
*<br> return type:
*/
public void Send (BufferedReader in, BufferedWriter out, String s) {
try {
Out.write (s + "\ n");
Out.flush ();
System.out.println (s);
s = in.readline ();
System.out.println (s);
}
catch (Exception e) {
E.printstacktrace ();
}
}
/**
*<br> Method Description: Overloaded method. Write information to the socket
*<br> input parameter: BufferedWriter out output buffer
*<br> input parameter: Information written by String s
*<br> return type:
*/
public void Send (BufferedWriter out, String s) {
try {
Out.write (s + "\ n");
Out.flush ();
System.out.println (s);
}
catch (Exception e) {
E.printstacktrace ();
}
}
}