0

How can I send an email with an attachment using Gmail's servers, but without JavaMail?

I have come across this code.

How to authenticate when using this code (using my gmail account as a sender)? Can this code be edited in a way that subject can be specified? I'm guessing that file.txt acts as an attachment?

EDIT: Here's the code from the link, adapted to my need's (more or less). Only difference is file location, smtp server and "from" and "to" strings.

public static void blabla() throws IOException, UnknownHostException {
        String msgFile = "C:\\file.txt";
        String from = "myemailaccount@gmail.com";
        String to = "otheremailaccount@gmail.com";
        String mailHost = "smtp.gmail.com";
        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");
        }

        @SuppressWarnings({"resource", "unused"})
        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;
        }
Ivan Karlovic
  • 221
  • 5
  • 14

1 Answers1

0

There are some tricks with sending email via GMails SMTP service.

Try having a read through

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I explicitly said WITHOUT JavaMail. – Ivan Karlovic Sep 22 '12 at 23:12
  • Yeah, but he's the clincher, they may provide you with some ideas. My first thought is SSL, the next is TLS. My first tip is, the ports not 25. Take a read, it might just point you in the right direction – MadProgrammer Sep 23 '12 at 03:40
  • You could also take a look at [SMTP Client with SSL/TLS](http://www.codeproject.com/Articles/98355/SMTP-Client-with-SSL-TLS). It's in C/C++, but the decision might be worth a read – MadProgrammer Sep 23 '12 at 03:44