Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:32:52+00:00 2026-05-26T03:32:52+00:00

I am sending mail from my Java app to Gmail Account. I had used

  • 0

I am sending mail from my Java app to Gmail Account. I had used the Java Mail API and it worked fine. But is it possible to send an e-mail without using the mail API in java?

I mean just by using sockets:

public class Main {
  public static void main(String[] args) throws Exception {
    String host = "smtp.gmail.com";
    int port = 465;
    String from = "sh2rpzain@gmail.com";
    String toAddr = "sharpzian@gmail.com";


    Socket servSocket = new Socket(host, port);
    DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
    DataInputStream is = new DataInputStream(servSocket.getInputStream());

    if (servSocket != null && os != null && is != null) {
      os.writeBytes("HELO\r\n");
      os.writeBytes("MAIL From:" + from + " \r\n");
      os.writeBytes("RCPT To:" + toAddr + "\r\n");
      os.writeBytes("DATA\r\n");
      os.writeBytes("X-Mailer: Java\r\n");
      os.writeBytes("DATE: " + DateFormat.getDateInstance(DateFormat.FULL, 
                                   Locale.US).format(new Date()) + "\r\n");
      os.writeBytes("From:" + from + "\r\n");
      os.writeBytes("To:" + toAddr + "\r\n");
    }

    os.writeBytes("Subject:\r\n");
    os.writeBytes("body\r\n");
    os.writeBytes("\r\n.\r\n");
    os.writeBytes("QUIT\r\n");
    String responseline;
    while ((responseline = is.readUTF()) != null) { 
      if (responseline.indexOf("Ok") != -1)
        break;
    }
  }
}

But it is not working, it doesn’t send out the mail. Can anyone tell me what could be the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:32:53+00:00Added an answer on May 26, 2026 at 3:32 am

    Here is a good example:

    public class SMTPDemo {
    
      public static void main(String args[]) throws IOException,
          UnknownHostException {
        String msgFile = "file.txt";
        String from = "java2s@java2s.com";
        String to = "yourEmail@yourServer.com";
        String mailHost = "yourHost";
        SMTP mail = new SMTP(mailHost);
        if (mail != null) {
          if (mail.send(new FileReader(msgFile), from, to)) {
            System.out.println("Mail sent.");
          } else {
            System.out.println("Connect to SMTP server failed!");
          }
        }
        System.out.println("Done.");
      }
    
      static class SMTP {
        private final static int SMTP_PORT = 25;
    
        InetAddress mailHost;
    
        InetAddress localhost;
    
        BufferedReader in;
    
        PrintWriter out;
    
        public SMTP(String host) throws UnknownHostException {
          mailHost = InetAddress.getByName(host);
          localhost = InetAddress.getLocalHost();
          System.out.println("mailhost = " + mailHost);
          System.out.println("localhost= " + localhost);
          System.out.println("SMTP constructor done\n");
        }
    
        public boolean send(FileReader msgFileReader, String from, String to)
            throws IOException {
          Socket smtpPipe;
          InputStream inn;
          OutputStream outt;
          BufferedReader msg;
          msg = new BufferedReader(msgFileReader);
          smtpPipe = new Socket(mailHost, SMTP_PORT);
          if (smtpPipe == null) {
            return false;
          }
          inn = smtpPipe.getInputStream();
          outt = smtpPipe.getOutputStream();
          in = new BufferedReader(new InputStreamReader(inn));
          out = new PrintWriter(new OutputStreamWriter(outt), true);
          if (inn == null || outt == null) {
            System.out.println("Failed to open streams to socket.");
            return false;
          }
          String initialID = in.readLine();
          System.out.println(initialID);
          System.out.println("HELO " + localhost.getHostName());
          out.println("HELO " + localhost.getHostName());
          String welcome = in.readLine();
          System.out.println(welcome);
          System.out.println("MAIL From:<" + from + ">");
          out.println("MAIL From:<" + from + ">");
          String senderOK = in.readLine();
          System.out.println(senderOK);
          System.out.println("RCPT TO:<" + to + ">");
          out.println("RCPT TO:<" + to + ">");
          String recipientOK = in.readLine();
          System.out.println(recipientOK);
          System.out.println("DATA");
          out.println("DATA");
          String line;
          while ((line = msg.readLine()) != null) {
            out.println(line);
          }
          System.out.println(".");
          out.println(".");
          String acceptedOK = in.readLine();
          System.out.println(acceptedOK);
          System.out.println("QUIT");
          out.println("QUIT");
          return true;
        }
      }
    }
    

    -> http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried sending mail with python from gmail and it works fine. But the
I need to send mail using Gmail SMTP, but it's showing Failure sending mail.
I am attempting to send mail through my gmail account from a Dedicated Godaddy
I am sending mail from .net application, my mail code is working fine but
I'm sending mail from my C# Application, using the SmtpClient. Works great, but I
sending mail along with embedded image using asp.net I have already used following but
I am using the following code for sending e-mail (gmail) using Java program. I
I am sending email using Java Mail API. When the email is received in
for this app i'm following this example: http://pipoltek.blogspot.com/2008/02/sending-mail-using-gmail-smtp-server.html I can send emails, it looks
i had succesfully sending an mail from ESS to the outlook express in standalone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.