Author Topic: Java SendMail program Source Code  (Read 825 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Java SendMail program Source Code
« on: December 05, 2011, 06:55:29 pm »
Here is send mail program source code using SMTP protocol. Note that host name is hard coded with gmail.com smtp server. So either from or to address must be gmail.com address.


Code: (Java)
//Source Code Listing

import java.io.*;
import java.net.*;

public class SendMail {
    public static void main(String[] args) {

        Socket client = null;  
        DataOutputStream outmessage = null;
        DataInputStream inmessage = null;
        
        if(args.length < 5)
        {
            System.out.println("Use Syntax: SendMail <From> <To> <Subject> <Message>");
            return;
        }
        String argFrom = args[0];
        String argTo = args[1];
        String argSubject = args[2];
        String argMessage = args[3];

        try {
            client = new Socket("alt2.gmail-smtp-in.l.google.com", 25);
            outmessage = new DataOutputStream(client.getOutputStream());
            inmessage = new DataInputStream(client.getInputStream());
        
            if (client != null && outmessage != null && inmessage != null) 
            {
                outmessage.writeBytes("HELO kathir\n");        
                String from = "MAIL From:" + argFrom + "\n";
                outmessage.writeBytes(from);
                String responseLine = inmessage.readLine();
                System.out.println("Server: " + responseLine);
                if (responseLine.indexOf("Ok") != -1) {
                      System.out.println("Invalid From Address");
                }
                
                String rcpt = "RCPT To:" + argTo + "\n";
                outmessage.writeBytes(rcpt);
                responseLine = inmessage.readLine();
                System.out.println("Server: " + responseLine);
                if (responseLine.indexOf("Ok") != -1) {
                      System.out.println("Invalid Recipient Address: " + argTo);
                }
                
                String subject = "Subject:" + argSubject + "\n";
                String message = argMessage;

                outmessage.writeBytes("DATA\n");
                outmessage.writeBytes(from);
                outmessage.writeBytes(subject);
                outmessage.writeBytes(message); 
                outmessage.writeBytes("\n.\n");
                outmessage.writeBytes("QUIT");
                
                responseLine = inmessage.readLine();
                System.out.println("Server: " + responseLine);
                if (responseLine.indexOf("Ok") != -1) 
                {
                      System.out.println("Error in sending email");;
                }
                
                outmessage.close();
                inmessage.close();
                client.close();   
            }
            
        } 
        catch (UnknownHostException e) 
        {
            System.err.println("gmail.com does not exist");
        }
        catch (IOException e) 
        {
            System.err.println("I/O exception while sending email");
        }
    } 
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1