0

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.

asked Jul 31, 2011 at 15:21
3
  • If it is only the class path dependency concern stopping you from using JavaMail then you can bundle JavaMail into your application jar. Eclipse can do this for you by creating a runnable jar - other IDEs like Netbeans might need a plugin. Commented Jul 31, 2011 at 18:35
  • Wow, didn't know that :) But size is also a factor. How much space do you think bundling along only the absolute necessities of JavaMail will take? Commented Jul 31, 2011 at 19:31
  • The size of mail.jar (it is javamail-1.4.5) is 495 KB only Commented Jan 8, 2013 at 4:58

2 Answers 2

2

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());
 }
answered Jul 31, 2011 at 15:38
Sign up to request clarification or add additional context in comments.

4 Comments

Some questions: What is the point of reading from the buffered reader if you never with whatever has been read? Also, why "HELLO" ? Are these phrases something that all smtp servers recognize and can act on?
@Mike Haye -- yes. the br is there to simulate conversation. I actually had the HELLO misspelled. Its HELO. Here is the list of recognized smtp messages .. networksorcery.com/enp/protocol/smtp.htm
what's os? I mean, what object os refers to?
@Ripon -- os is OutputStream .. OutputStream os = socket.getOutputStream();
0

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);
 }
 }
}
answered Jul 31, 2011 at 15:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.