Results 1 to 3 of 3
- 04-26-2011, 09:10 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
send mail via java without java mail API
hi every body.
first of all excuse me for my bad english
i want to send a mail via java using smtp. i shoudl implement it.
i did it but when i run my program it has an error.
i put the execution picture here... i shoud i have a smtp server fro my program but i dont know what does for it... is there any body to help me...
- 04-26-2011, 09:14 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
this is my Main Class program
//package rblasch.Mail;
/**
* Contains <em>Main</em>.
*
* <p>This is my solution of program assignment #2. The documentation can be
* found at <a href="http://www.cosy.sbg.ac.at/~rblasch/study/cs629/pa2/">
* http://www.cosy.sbg.ac.at/~rblasch/study/cs629/pa2/</a>.</p>
* @author Ronald Blaschke <rblasch@cs.bgsu.edu>
* @version 1.0
*/
public class Client2
{
/**
* Main for a very simple SMTP client.
*
* <p>Usage is:
* <tt>java Client2 <server_name> <server_port> <message
* sender> <Message receiver> <message></tt></p>
*
* <dl>
* <dt>server_name
* <dd>Name of the host where the SMTP server is running.
* <dt>server_port
* <dd>Port number of the SMTP server (25).
* <dt>message_sender
* <dd>email address of mail sender
* <dt>message_receiver
* <dd>email address of mail receiver
* <dt>message
* <dd>The message to send.
* </dl>
*
* <p>The debugFlag of the SmtpMail class is turned on.</p>
*
* <h2>Sample in/output (with debug info)</h2>
* <p><pre>
alpha 103% java Client2 conductor.cs.bgsu.edu 25 'rblasch@cs.bgsu.edu' 'rblasch@cs.bgsu.edu' "MFG to me!"
D:trying to connect to server
D:connected
D:response: 220 conductor.cs.bgsu.edu ESMTP Sendmail 8.9.1/8.9.1; Sun, 3 Oct 1999 01:50:33 -0400 (EDT)
D:sending command: HELO alpha.bgsu.edu
D:response: 250 conductor.cs.bgsu.edu Hello alpha.bgsu.edu [129.1.2.12], pleased to meet you
D:sending command: MAIL from: rblasch@cs.bgsu.edu
D:response: 250 rblasch@cs.bgsu.edu... Sender ok
D:sending command: RCPT to: <rblasch@cs.bgsu.edu>
D:response: 250 <rblasch@cs.bgsu.edu>... Recipient ok
D:sending command: DATA
D:response: 354 Enter mail, end with "." on a line by itself
D:MFG to me!
D:.
D:response: 250 BAA23326 Message accepted for delivery
D:sending command: QUIT
D:response: 221 conductor.cs.bgsu.edu closing connection
* </pre></p>
*
* @author Ronald Blaschke <rblasch@cs.bgsu.edu>
* @version 1.0
*/
public static void main(String[] argv)
{
/*if (argv.length < 5) {
System.out.println("usage: java Client2 <server_name> <server_port> " +
"<message sender> <message receiver> <message>");
System.exit(1);
}
*/
String serverName ="www.argosoft.com";
int serverPort = Integer.parseInt("25");
MailAddress sender = MailAddress.parseAddress("majidvalipour@yaoo.com") ;
MailAddress receiver = MailAddress.parseAddress("alone_3619@yahoo.com");
String msg = "Hello Every Body";
SmtpMail smtp = new SmtpMail();
smtp.debugMode(true);
try {
smtp.simpleSend(serverName, serverPort, sender, receiver,
"Mail from Client2", msg);
}
catch (Exception e) {
System.err.println("Error while sending: " + e.toString());
System.exit(1);
}
System.exit(0);
}
}
- 04-26-2011, 09:30 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
this is my MailAddress class
//package rblasch.Mail;
/**
* MailAddress contains the information <em>address</em> and
* <em>realName</em>.
*
* <p>adress is the email address,
* e.g. <samp>rblasch@cs.bgsu.edu</samp>. realName would be <samp>Ronald
* Blaschke</samp> The email address is <b>required</b>, the realName is
* optional.</p>
*
* <p>The string representation is as follows:</p>
* <dl>
* <dt>email address only
* <dd><<var>email address</var>>
* <dt>email address and real life name
* <dd>"<var>Real Life Name</var>" <<var>email address</var>>
* </dl>
*
* @author Ronald Blaschke <rblasch@cs.bgsu.edu>
* @version 1.0
*/
public class MailAddress
{
/**
* Create new Object.
*/
public MailAddress()
{
super();
}
/**
* Convert a String to a MailAddress. Currently the String may only contain
* the email address, eg <samp>rblasch@cs.bgsu.edu</samp>.
*
* @param address String containing the address
* @return The MailAddress
*/
public static MailAddress parseAddress(String address)
{
MailAddress addr = new MailAddress();
addr.setAddress(address);
return addr;
}
/**
* Sets the email address part.
*
* @param address email address
*/
public void setAddress(String address)
{
this.address = address;
}
/**
* Gets the email address part.
*
* @return email address
*/
public String getAddress() {
return address;
}
/**
* Sets the real life name part.
*
* @param realName real life name
*/
public void setRealName(String realName)
{
this.realName = realName;
}
/**
* Gets the real life name part.
*
* @return real life name
*/
public String getRealName() {
return realName;
}
/**
* Converts this object to a String; see class description for format.
*
* @return String representation of this MailAddress
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
// real life name is OPTIONAL
if (realName != null) {
buff.append("\"" + realName + "\" ");
}
// address is REQUIRED
if (address != null) {
buff.append("<" + address + ">");
}
else {
return null;
}
return buff.toString();
}
/**
* The email address. Eg <samp>rblasch@cs.bgsu.edu</samp>.
*/
private String address = null;
/**
* The real life name. Eg <samp>Ronald Blaschke</samp>.
*/
private String realName = null;
}
tihs is my SmtpException class
//package rblasch.Mail;
/**
* This class is used to indicate an error while communicating with an SMTP
* server.
*
* @author Ronald Blaschke <rblasch@cs.bgsu.edu>
* @version 1.0
*/
public class SmtpException extends Exception
{
/**
* Create a new Exception.
*
* @param lastCmd The last command sent before the error was recognized.
* @param errorCode The errorcode returned by the SMTP server.
* @param errorMsg The errormessage (from SMTP server).
*/
public SmtpException(String lastCmd, int errorCode, String errorMsg)
{
this.lastCmd = lastCmd;
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
/**
* Convert Exception to String.
*
* <p>Format is <code>Error while executing cmd <var><command></var>:
* <var><errorCode></var> - <var><errorMessage></var>.</p>
*
* @return String representation of Exception
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append("Error while executing cmd " + lastCmd + ":"
+ errorCode + "-" + errorMsg);
return buff.toString();
}
String lastCmd = null;
int errorCode = -1;
String errorMsg = null;
}
and this is the SmtpMail class
//package rblasch.Mail;
import java.io.*;
import java.net.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;
/**
* This class implements the SMTP protocol.
*
* <p>This implementation is very limited. The only way to send is
* <code>simpleSend</code>; see method description for details.</p>
*
* <p>The expected message format is: lines separated by newline
* <code>'\n'</code> characters.</p>
*
* <p>You may turn on the debug flag via <code>debugMode(true)</code>; debug
* output goes to stderr.</p>
*
* @see RFC821, RFC974, RFC822
* @author Ronald Blaschke <rblasch@cs.bgsu.edu>
* @version 1.0
*/
public class SmtpMail
{
/**
* Create new Object.
*/
public SmtpMail()
{
super();
rcpts = new Vector();
}
/**
* Sets the debug mode.
*
* @param debug true for debug mode, otherwise false (default).
*/
public void debugMode(boolean debug)
{
this.debug = debug;
}
/**
* Outputs the String to stderr if debug is turned on.
*
* @param t String to trace.
*/
protected void trace(String t)
{
if (debug) {
System.err.println("D:" + t);
}
}
/**
* Minimalistic send method.
*
* @param serverName Address of SMTP server (numerical or by name)
* @param port SMTP port number (should be 25)
* @param sender Address of mail sender
* @param receiver Address of mail receiver
* @param subject Subject of message
* @param msg The message; line seperator is <code>'\n'</code>.
* @exception UnknownHostException Unknown host
* @exception IOException Error while communicating
* @exception SmtpException Error returned by SMTP server
*
* @see #send
*/
public void simpleSend(String serverName, int port, MailAddress sender,
MailAddress receiver, String subject, String msg)
throws SmtpException, UnknownHostException, IOException
{
serverAddress = InetAddress.getByName(serverName);
serverPort = port;
this.sender = sender;
rcpts.addElement(receiver);
this.subject = subject;
message = msg;
send();
}
/**
* Sends a message to the SMTP server.
*
* <p>Sending is done via the following sequence:</p>
* <ol>
* <li>Connect to server
* <li>HELO
* <li>MAIL FROM
* <li>RCPT TO
* <li>DATA
* <li>...data...
* <li>QUIT
* </ol>
*
* <p>The following limitations apply:</p>
* <ul>
* <li>No retry logic; all failures are considered as errors, although some
* may be recovered.
* <li>Message is sent to the first receiver only.
* <li>Header should contain more that <code>From</code>, <code>To</code>
* and <code>Subject</code> (eg DATE, MIME type).
* </ul>
*
* @exception IOException Error while communicating
* @exception SmtpException Error returned by SMTP server
*/
protected void send()
throws SmtpException, IOException
{
Socket sock = null;
try {
// setup connection
trace("trying to connect to server");
sock = new Socket(serverAddress, serverPort);
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter out = new PrintWriter(sock.getOutputStream());
trace("connected");
int rc = getResponse(in);
if (rc/100 != 2) {
throw new SmtpException("CONNECT", rc, lastResponseText);
}
// connected, be nice and say hello
sendCommand(out, "HELO " + InetAddress.getLocalHost().getHostName(),
in, 2);
sendCommand(out, "MAIL from: " + sender.getAddress(),
in, 2);
// XXX only implemented sending to first rcpt
sendCommand(out, "RCPT to: " + ((MailAddress)(rcpts.firstElement())).getAddress() , in, 2);
sendCommand(out, "DATA", in, 3);
sendData(out);
rc = getResponse(in);
if (rc/100 != 2) {
throw new SmtpException("SEND-DATA", rc, lastResponseText);
}
sendCommand(out, "QUIT", in, 2);
}
catch (IOException e1) {
if (sock != null) sock.close();
/*re*/throw e1;
}
catch (SmtpException e2) {
if (sock != null) sock.close();
/*re*/throw e2;
}
}
/**
* Sends a single command to the SMTP server.
*
* @param out Outputstream to SMTP server
* @param cmd Command to send, without trailing <code><CR/LF></code>.
*/
protected void sendCommand(PrintWriter out, String cmd)
{
trace("sending command: " + cmd);
out.write(cmd);
out.write("\r\n");
out.flush();
}
/**
* Sends a command and throws a SMTPException if the returncode is not in the
* expected class, ie the first digit of the returncode matches.
*
* @param out Outputstream to SMTP server
* @param cmd Command to send, without trailing <code><CR/LF></code>.
* @param in Inputstream to SMTP server
* @param OkClass Class of returncodes that is ok
* @exception SmtpException Thrown if answer not in expected returncode
* class.
* @exception IOException Error while communicating
*/
protected void sendCommand(PrintWriter out, String cmd, BufferedReader in,
int OkClass)
throws SmtpException, IOException
{
sendCommand(out, cmd);
int rc = getResponse(in);
if (rc/100 != OkClass) {
throw new SmtpException(cmd, rc, lastResponseText);
}
}
/**
* Sends the data to the SMTP server.
*
* <p>First a simple header, containing the <var>From</var> and <var>To</var>
* field is sent. The follows the <var>Subject</var>. After that we convert
* the message to the data format and send it. We finish the data with a
* <code><CR/LF>.<CR/LF></code> sequence.</p>
*
* @param out Outputstream to SMTP server
*/
protected void sendData(PrintWriter out)
{
// send header
out.write("From: " + sender + "\r\n");
out.write("To: " + rcpts.firstElement() + "\r\n");
out.write("Subject: " + subject + "\r\n");
out.write("\r\n"); // end header
// send message text
String data = msg2data(message);
trace(data);
out.write(data);
// end data with <CR/LF>.<CR/LF>
trace(".");
out.write("\r\n.\r\n");
out.flush();
}
/**
* Converts a message to the data format.
*
* <p>The expected message format is: lines separated by newline
* (<code>'\n'</code>) characters. The SMTP data format is: lines separated
* by <code><CR/LF></code>; if a message line begins with a period
* another period is added in front of this line.</p>
*
* @param msg Message to convert
* @return The converted message
*/
protected String msg2data(String msg)
{
StringBuffer buff = new StringBuffer();
String line;
int start=0;
int end=0;
if (msg != null) {
buff.ensureCapacity(msg.length()+100);
do {
end = msg.indexOf('\n', start);
if (end == -1) {
line = msg.substring(start);
}
else {
line = msg.substring(start, end);
end++; // skip newline character
}
if (line.length() > 0 && line.charAt(0) == '.') {
buff.append('.');
}
buff.append(line);
if (end != -1) {
buff.append("\r\n");
}
start = end;
} while (end != -1);
}
return buff.toString();
}
/**
* Gets a response from the SMTP server.
*
* <p>Each line begins with a returncode, followed by either a space or a
* dash (<code>'-'</code>). A dash indicates that there are following lines,
* a space that this is the last line. The rest of a line is addition text.
* Lines are separated by <code><CR/LF></code>.</p>
*
* @param in Inputstream to SMTP server
* @return Returncode from SMTP server
* @exception IOException Error while reading from server
*/
protected int getResponse(BufferedReader in)
throws IOException
{
int responseCode;
boolean moreLines;
String line;
StringBuffer text = new StringBuffer();
do {
// check if there are more lines and, if yes, skip them
line = in.readLine();
trace("response: " + line);
moreLines = (line.charAt(3) == '-');
text.append(line.substring(4, line.length()));
} while (moreLines);
// last line; chars 0-2 contain returncode, char 3 is space, rest is additional text
responseCode = Integer.parseInt(line.substring(0, 3));
// store last response
lastResponseText = text.toString();
return responseCode;
}
/**
* The internet address of the SMTP server
*/
private InetAddress serverAddress = null;
/**
* The port on the host the SMTP server is running.
*/
private int serverPort = -1;
/**
* Sender address.
*/
private MailAddress sender = null;
/**
* Receivers.
*/
private Vector rcpts = null;
/**
* Mail subject.
*/
private String subject = null;
/**
* The message. The expected message format is: lines separated by newline
* <code>'\n'</code> characters.
*/
private String message = null;
/**
* Text of the last received response of server.
*/
private String lastResponseText = null;
/**
* Debugging?
*/
private boolean debug = false;
}
this is my output
D:trying to connect to server
D:connected
D:response: 220 Exchange.NOSA.com Microsoft ESMTP MAIL Service ready at Wed, 27
Apr 2011 02:16:28 +0430
D:sending command: HELO majid
D:response: 250 Exchange.NOSA.com Hello [78.38.150.220]
D:sending command: MAIL from: majidvalipour@yaoo.com
D:response: 250 2.1.0 Sender OK
D:sending command: RCPT to: alone_3619@yahoo.com
D:response: 550 5.7.1 Unable to relay
Error while sending: Error while executing cmd RCPT to: alone_3619@yahoo.com:550
-5.7.1 Unable to relay
Press any key to continue . . .
please help me
i know problem is from smtpserver..
Similar Threads
-
How to identify received mail is failure delivery notice mail in javamail?
By satheeshtech in forum Advanced JavaReplies: 2Last Post: 07-25-2009, 09:36 AM -
Attachments were missing when mail was sent through java mail API
By Malathi in forum Web FrameworksReplies: 2Last Post: 06-04-2009, 01:42 PM -
Help, Please!!! Can't Send Mail with SmtpClient
By jfcup in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-07-2008, 12:46 PM -
Javax.mail.MethodNotSupported Exception in java mail api
By namarc in forum Advanced JavaReplies: 2Last Post: 05-05-2008, 06:01 AM -
Send a pic through mail, in java
By lenny in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks