Home arrow static arrow Java Programming [Archive] - Java Mail( not able to send the mail to ouside accounts)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java Mail( not able to send the mail to ouside accounts)
1 Duke Stars available
This topic has 4 replies on 1 page.

Registered: 8/17/03
Java Mail( not able to send the mail to ouside accounts)  
Aug 4, 2004 11:54 PM


I am able to send the mail to local people (belongs to same email server) by my SMTP server but when i am sending the mail to outside the account like ( any yahoo id or rediffmail id ) i am getting the following error. ( all yahoo or rediffmail Ids are correct)

can any body help me ? Thanks in advance...

Error shown by the program

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 not local host rediffmail.com, not a gat

at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at sendhtml.<init>(sendhtml.java:191)
at sendhtml.main(sendhtml.java:60)


( same program as in demo directory of Javamail(sendhtml.java))
slight modification to send gif file also

import java.io.*;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;

public class sendhtml {

public static void main(String[] argv) {
new sendhtml(argv);

public sendhtml(String[] argv) {

String to, subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String mailer = "sendhtml";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind;

for (optind = 0; optind < argv.length; optind++) {
if (argv[optind].equals("-T")) {
protocol = argv[++optind];
} else if (argv[optind].equals("-H")) {
host = argv[++optind];
System.out.println("host is "+host);
} else if (argv[optind].equals("-U")) {
user = argv[++optind];
} else if (argv[optind].equals("-P")) {
password = argv[++optind];
} else if (argv[optind].equals("-M")) {
mailhost = argv[++optind];
} else if (argv[optind].equals("-f")) {
record = argv[++optind];
} else if (argv[optind].equals("-s")) {
subject = argv[++optind];
} else if (argv[optind].equals("-o")) { // originator
from = argv[++optind];
} else if (argv[optind].equals("-c")) {
cc = argv[++optind];
} else if (argv[optind].equals("-b")) {
bcc = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-d")) {
debug = true;
} else if (argv[optind].equals("--")) {
} else if (argv[optind].startsWith("-")) {
"Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
"\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
"\t[-f record-mailbox] [-M transport-host] [-d] [address]");
} else {

try {
if (optind < argv.length) {
// XXX - concatenate all remaining arguments
to = argv[optind];
System.out.println("To: " + to);
} else {
System.out.print("To: ");
to = in.readLine();
if (subject == null) {
System.out.print("Subject: ");
subject = in.readLine();
} else {
System.out.println("Subject: " + subject);

Properties props = System.getProperties();
// XXX - could use Session.getTransport() and Transport.connect()
// XXX - assume we're using SMTP
System.out.println("mail host ="+mailhost);
if (mailhost != null)

//props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.host", mailhost);

// Get a Session object
Session session = Session.getInstance(props, null);
if (debug)

// construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));

InternetAddress.parse(to, false));
if (cc != null)
InternetAddress.parse(cc, false));
if (bcc != null)
InternetAddress.parse(bcc, false));

System.out.println("Calling collect");
collect(in, msg);

msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());

// send the thing off

System.out.println("\nMail was sent successfully.");

// Keep a copy, if requested.

if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
} else {
if (protocol != null)
store = session.getStore(protocol);
store = session.getStore();

// Connect
if (host != null || user != null || password != null)
store.connect(host, user, password);

// Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
System.err.println("Can't get record folder.");
if (!folder.exists())

Message[] msgs = new Message[1];
msgs[0] = msg;

System.out.println("Mail was recorded successfully.");

} catch (Exception e) {

public void collect(BufferedReader in, Message msg)
throws MessagingException, IOException {
String line;
String subject = msg.getSubject();
StringBuffer sb = new StringBuffer();

MimeMultipart mp = new MimeMultipart();


MimeBodyPart mbp1= new MimeBodyPart();
String html =
" see the following jpg : it is a car!
"<IMG SRC=cid:23abc@pc27 width=10% height=10%>
" end of jpg"+
"<IMG SRC=cid:24abc@pc27 width=10% height=10%>
" end of jpg"+

/* <IMG SRC=cid:23abc@pc27 width=80% height=60%>
" end of jpg"+

mbp1.setDataHandler(new DataHandler(new ByteArrayDataSource(html, "text/html")));

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource("wiki/./image001.gif");

mbp2.setDataHandler(new DataHandler(fds));

MimeBodyPart mbp3 = new MimeBodyPart();
FileDataSource fds3 = new FileDataSource("wiki/./image002.jpg");

mbp3.setDataHandler(new DataHandler(fds3));




Registered: 17.04.98
Re: Java Mail( not able to send the mail to ouside accounts)  
Aug 5, 2004 12:40 AM (reply 1 of 4)

" 550 not local host rediffmail.com, not a gateway"

the server doesn't allow you to send mail to the outside.

Registered: 1/14/04
Re: Java Mail( not able to send the mail to ouside accounts)  
Aug 5, 2004 4:36 AM (reply 2 of 4)

Just use some other SMTP server that allows outside sending. That could be almost any of payfree servers.

Registered: 7/5/04
Re: Java Mail( not able to send the mail to ouside accounts)  
Aug 5, 2004 12:12 PM (reply 3 of 4)

Try some third party smtp software that will send the mail directly to the reciepents mail server.
Here is one

Registered: 5/23/97
Re: Java Mail( not able to send the mail to ouside accounts)  
Aug 5, 2004 3:33 PM (reply 4 of 4)


I am able to send the mail to local people (belongs to
same email server) by my SMTP server but when i am
sending the mail to outside the account like ( any
yahoo id or rediffmail id ) i am getting the following
error. ( all yahoo or rediffmail Ids are correct)

can any body help me ? Thanks in advance...

Here is a class from my HyperView API that does what you want...

// SendMail by Tony Swain. 1997.// this programs sends mail Via SMTP as defined in RFC 821.// ftp://ftp.isi.edu/in-notes/rfc821.txt import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.util.StringTokenizer; public class SendMail    {Object mailLock              = null;  //In case we want a multi-threaded mailer public String mailServerHost = "";public String from           = "";public String to             = "";public String replyTo        = "";public String subject        = "Java is Fun";public String mailData       = "HyperSendMail";public String errorMsg = "";public Socket mailSendSock = null;public  BufferedReader inputStream = null;public PrintStream outputStream    =  null;public String serverReply          = ""; SendMail() {   // Doesn't do anything but we need this for extension purposes.} // Server, from,to,subject, data SendMail(String server,String tFrom,String tTo,String sub,String sendData){   mailServerHost = server;   mailLock=this; // Thread Monitor passed constructor later. Default this Monitor.   from = tFrom;   to   = tTo;   if(sendData != null)      mailData = sendData;  } public void send(){   if(!open())          //Yikes! get out of here.      return;        try   {      outputStream.println("HELO sendMail");      serverReply = inputStream.readLine();     }   catch(Exception e0)   {      e0.printStackTrace();   }   try   {      outputStream.println("MAIL FROM: "+from);      serverReply = inputStream.readLine();        // I cheat and don't look for the whole 550        // we know 5 is an error anyway. Add it in if you want.        //      if(serverReply.startsWith("5"))      {         close("FROM: Server error :"+serverReply);         return;      }           // Note the switch here. we could get mail from somewhere and by   // pre setting replyTo reply somewhere else :)       if(replyTo == null)         replyTo = from;      outputStream.println("RCPT TO: <">");       //  Skipped over  250 OK messages. Add it in if you really want.       //  but we  assume if  we didn't get an error, then it must be OK.      serverReply = inputStream.readLine();      if(serverReply.startsWith("5"))      {         close("Reply error:"+serverReply);         return;      }      outputStream.println("DATA");      serverReply = inputStream.readLine();      if(serverReply.startsWith("5"))      {         close("DATA Server error : "+serverReply);         return;      }      outputStream.println("From: "+from);      outputStream.println("To: "+to);      if(subject != null)         outputStream.println("Subject: "+subject);      if(replyTo != null)         outputStream.println("Reply-to: "+replyTo);      outputStream.println("");      outputStream.println(mailData);      outputStream.print("\r\n.\r\n");      outputStream.flush();      serverReply = inputStream.readLine();      if(serverReply.startsWith("5"))      {         close("DATA finish server error: "+serverReply);         return;      }      outputStream.println("quit");      serverReply = inputStream.readLine();      if(serverReply.startsWith("5"))      {         close("Server error on QUIT: "+serverReply);         return;      }      inputStream.close();      outputStream.close();      mailSendSock.close();   }   catch(Exception any)   {      any.printStackTrace();      close("send() Exception");   }   close("Mail sent");} public boolean open(){   synchronized(mailLock)   {      try      {         mailSendSock = new Socket(mailServerHost, 25);         outputStream = new PrintStream(mailSendSock.getOutputStream());         inputStream = new BufferedReader(new InputStreamReader(          mailSendSock.getInputStream()));         serverReply = inputStream.readLine();         if(serverReply.startsWith("4"))         {            errorMsg = "Server refused the connect message : "+serverReply;            return false;         }      }      catch(Exception openError)        {         openError.printStackTrace();         close("Mail Socket Error");         return false;      }      System.out.println("Connected to "+mailServerHost);      return true;   } }  public void close(String msg){          //try to close the sockets   System.out.println("Close("+msg+")");   try   {      outputStream.println("quit");      inputStream.close();      outputStream.close();      mailSendSock.close();   }   catch(Exception e)   {      System.out.println("Close() Exception");     // We are closing so see ya later anyway   }} // Uncomment for application mode.   /*public static void main(String Args[]){SendMail sm = new // * NOTE:// Erase these values right away! Just to show you how it is done.// Whatever you do don' release it with my mail server hardcoded.// last thing I need is 10 million Java mail test spams :) SendMail(          "mail.somewhere.com",         //Mail Server          "you@yourHost.com",       // sender          "recipient@theirMail.com",       // Recipient          "THis is the subject",               // Subject          "This is the mail data. Put your letter here.");             // Message Data          sm.send();                      // Send it!}*/ }

That was whipped up in a hurry, but it does function.
At least it is a fair place to start fun with RFC 821.

However, iIf your mail server is not configured to handle relaying,
then this is your problem.
(Really not nice to set up )
One day I am going to rewrite this in NIO BTW.
Have fun!
This topic has 4 replies on 1 page.