开发者

Sending an e-mail in java without the JavaMail API

I'm developing an application, where users have the option to have sent an e-mail to a specified e-mail every x minutes.

I don't want to rely on JavaMail (i.e. rely on whether my users have added the JavaMail jar to their classpath).

I realize that I could go on and create a server for doing this and connect to it with the necessary details, but this is last option.

How would I go on sending an e-mail in this case?

Are there any online services etc (paid or free) that provides a solution for this? For example connecting to them and specifying recipient e-mail and message, they would handle the e-mail sending.

Are there any smart and/or reasonably easy ways of sending e-mails using the开发者_开发技巧 Java Core packages?

Thanks :)

Mike.


You can -- by opening a socket to the smtp server and then writing to that socket.

Socket socket=new Socket("your.smtp.server",25);
br= new BufferedReader(newInputStreamReader(socket.getInputStream()));
os = socket.getOutputStream();
    smtp("HELLO " + toEmailAddress);
smtp("MAIL FROM: "+ fromEmailAddress);
smtp("DATA");
smtp(yourContent");

and your smtp method would just read from the bufferedreader and write to socket

    public void smtp(String command) { 
           br.readLine();
           os.write(command.getBytes());
    }


Here is some old code I had lying around that might get you started:

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

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

        if ( args.length != 5 )
        {
            System.out.print("usage: java EMail2 <smtp-host> <fromName> <toAddress>");
            System.out.println(" <subject> <body>");
            System.exit(-1);
        }

        try
        {
            send(args[0], args[1], args[2], args[3], args[4]);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        System.exit(0);
    }

    public static void send(String host, String from, String to, String subject, String message)
    {
        try
        {
            System.setProperty("mail.host", host);
//          System.setProperty("mail.smtp.starttls.enable","true"); // not sure it this works or not

          // open connection using java.net internal "mailto" protocol handler
          URL url = new URL("mailto:" + to);
          URLConnection conn = url.openConnection();
          conn.connect();

          // get writer into the stream
          PrintWriter out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream() ) );

          // write out mail headers
          // From header in the form From: "alias" <email>
          out.println("From: \"" + from + "\" <" + from + ">");
          out.println("To: " + to);
          out.println("Subject: " + subject);
          out.println(); // blank line to end the list of headers

          // write out the message
          out.println(message);

          // close the stream to terminate the message
          out.close();
        }
        catch(Exception err)
        {
          System.err.println(err);
        }
      }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜