DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    anju Guest

    sending mail thro' smtp server


    I am trying to send mail thro' smtp server but i am getting mail with no body
    kindly help
    I am pasting the code also


    import java.io.*;
    import java.net.*;
    public class processMessage {
    static PrintWriter streamOut;
    static DataInputStream dis;
    public processMessage()
    {
    String HELO = "HELO ";
    String MAIL_FROM = "mail from:anjuagg@hotmail.com";
    String RCPT_TO = "rcpt to:anjua@niit.com";
    String DATA = "data";
    String SUBJECT = "subject:Subject goes here";
    String BODY = "Message goes here" +"\n.\nquit";
    String QUIT = "quit";

    Socket smtp = null;

    try{
    System.out.println("in the constructor");
    smtp = new Socket("mail2.niit.com",25);
    OutputStream os = smtp.getOutputStream();
    streamOut = new PrintWriter(os,true);
    InputStream is = smtp.getInputStream();
    dis = new DataInputStream(is);
    }
    catch (IOException e){
    System.out.println("Error connecting: "+e);
    }

    try{
    String loc = InetAddress.getLocalHost().getHostName();
    send(HELO +loc);
    receive();
    send(MAIL_FROM);
    receive();
    send(RCPT_TO);
    receive();
    send(DATA);
    receive();
    send(SUBJECT);
    receive();
    send(BODY);
    receive();
    receive();//get closing response
    smtp.close();
    }
    catch (IOException e){
    System.out.println("Error sending: "+e);
    }
    System.out.println("Message Sent!");
    }

    public static void main(String args[])
    {
    System.out.println("1");
    processMessage pro = new processMessage();
    System.out.println("2");
    }
    public static void send(String str) throws IOException {
    streamOut.write(str+"\n");
    streamOut.flush();
    System.out.println("Java sent: "+str);
    }

    public static void receive() throws IOException {
    String readstr = dis.readLine();
    System.out.println("SMTP Response: "+readstr);
    }
    }

    regards
    anju



  2. #2
    Satish Pamidimarthi Guest

    Re: sending mail thro' smtp server


    Dear Anju,
    dear... go for java.mail package instead of all this mess.. here
    is a sample code...!
    =====================Beginning of Java Code===============
    import java.io.*;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.event.*;
    import javax.mail.internet.*;

    public class MailTest
    {
    public static void main(String args[])
    {
    MailTest mt = new MailTest();
    mt.mailPlease();
    }

    public void mailPlease()
    {
    String mailHost = "yourmailhost.com";
    String source = "username@yourmailhost.com";
    String destination = "username@destmailhost.com";



    Properties properties = new Properties();

    properties.put("mail.smtp.host", mailHost);
    properties.put("mail.from", source);

    Session session = Session.getInstance( properties, null );

    try
    {
    Message message = new MimeMessage( session );
    InternetAddress[] address = { new InternetAddress( destination )};

    message.setRecipients( Message.RecipientType.TO, address );
    message.setFrom( new InternetAddress( source) );
    message.setSubject( "re: This is Java Mail API Test");
    message.setContent( "Hello, World, This JAVA Mail API Test", "text/plain");

    Transport transport = session.getTransport( address[0] );
    transport.addConnectionListener( new ConnectionHandler() );
    transport.addTransportListener( new TransportHandler() );
    transport.connect();
    transport.sendMessage( message, address );
    }
    catch( Exception ex )
    {

    System.out.println( ex.getMessage() );
    }
    }

    class ConnectionHandler extends ConnectionAdapter
    {
    public void opened( ConnectionEvent e )
    {
    System.out.println("Connection opened...!");
    }
    public void disconnected(ConnectionEvent e )
    {
    System.out.println("Connection disconnected.");
    }

    public void closed(ConnectionEvent e )
    {
    System.out.println("Connection Closed...!");
    }
    }

    class TransportHandler extends TransportAdapter
    {
    public void messageDelivered(TransportEvent e)
    {
    System.out.println("Message Delivered...!");
    }

    public void messageNotDelivered(TransportEvent e )
    {
    System.out.println("Message NOT delivered...!");
    }

    public void messagePartiallyDelivered(TransportEvent e )
    {
    System.out.println("Message PARTIALLY delivered...!");
    }
    }
    }
    =====================End of Java Code=====================

    "anju" <anjua@niit.com> wrote:
    >
    >I am trying to send mail thro' smtp server but i am getting mail with no

    body
    >kindly help
    >I am pasting the code also
    >
    >
    >import java.io.*;
    >import java.net.*;
    >public class processMessage {
    > static PrintWriter streamOut;
    > static DataInputStream dis;
    >public processMessage()
    >{
    > String HELO = "HELO ";
    > String MAIL_FROM = "mail from:anjuagg@hotmail.com";
    > String RCPT_TO = "rcpt to:anjua@niit.com";
    > String DATA = "data";
    > String SUBJECT = "subject:Subject goes here";
    > String BODY = "Message goes here" +"\n.\nquit";
    > String QUIT = "quit";
    >
    > Socket smtp = null;
    >
    > try{
    > System.out.println("in the constructor");
    > smtp = new Socket("mail2.niit.com",25);
    > OutputStream os = smtp.getOutputStream();
    > streamOut = new PrintWriter(os,true);
    > InputStream is = smtp.getInputStream();
    > dis = new DataInputStream(is);
    > }
    > catch (IOException e){
    > System.out.println("Error connecting: "+e);
    > }
    >
    > try{
    > String loc = InetAddress.getLocalHost().getHostName();
    > send(HELO +loc);
    > receive();
    > send(MAIL_FROM);
    > receive();
    > send(RCPT_TO);
    > receive();
    > send(DATA);
    > receive();
    > send(SUBJECT);
    > receive();
    > send(BODY);
    > receive();
    > receive();//get closing response
    > smtp.close();
    > }
    > catch (IOException e){
    > System.out.println("Error sending: "+e);
    > }
    > System.out.println("Message Sent!");
    > }
    >
    >public static void main(String args[])
    >{
    > System.out.println("1");
    > processMessage pro = new processMessage();
    > System.out.println("2");
    >}
    >public static void send(String str) throws IOException {
    >streamOut.write(str+"\n");
    >streamOut.flush();
    >System.out.println("Java sent: "+str);
    >}
    >
    >public static void receive() throws IOException {
    >String readstr = dis.readLine();
    >System.out.println("SMTP Response: "+readstr);
    >}
    >}
    >
    >regards
    >anju
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links