We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Sending an Email Programmatically
Page Index Toggle Pages: 1
Sending an Email Programmatically? (Read 1409 times)
Sending an Email Programmatically?
Mar 3rd, 2006, 5:16am
 
Hi all -

I was wondering how difficult it would be to send an email from within Processing?  For example, if I wanted to have a physical button connected to a computer which autmatically emailed me (email address was predetermined) whenever the button was pressed?

I'm not very knowledgable about web protocols, and searching through Google so far has only left me with a lot of weird Outlook and Delphi links.

Thanks for any help!
Re: Sending an Email Programmatically?
Reply #1 - Mar 3rd, 2006, 10:42am
 
If you want processing to talk to a mail server to send e-mail you'll need: http://www.faqs.org/rfcs/rfc821.html

That's the document that defines the standard for sending e-mail that most programs and mail servers understand.
Re: Sending an Email Programmatically?
Reply #2 - Mar 18th, 2006, 7:23am
 
Sweet, thanks John.

I was able to find some SMTP code and get the project working.

Re: Sending an Email Programmatically?
Reply #3 - Mar 31st, 2006, 4:24pm
 
Hi Stoic

Fancy sharing that?

Cheers, Andy
Re: Sending an Email Programmatically?
Reply #4 - Mar 31st, 2006, 6:10pm
 
Or perhaps write it up here?  Sounds very useful!

http://www.processinghacks.com/hacks/sendingemail
Re: Sending an Email Programmatically?
Reply #5 - Apr 12th, 2006, 5:40pm
 
//This works without any extra libraries!

//Code adapted from http://www.javaworld.com/javaworld/javatips/jw-javatip36-p3.html

//and Craig Morrall <morrall at comp.lancs.ac.uk>

String sender = "<me at gold.ac.uk>";
String reciever = "<someone else at gold.ac.uk>";
String subject = "This message is coming from Processing";
String message = "Mail Message";
String mailServer = "postbox.gold.ac.uk";

//strange java thing to get a correct carriage return
String  carriageReturn = System.getProperty("line.separator");

void setup()
{
 sendMessage();
}

void sendMessage()
{
 println("Trying...");
 try {

   //connect to the mail server
   Socket socket = new Socket(mailServer, 25);

   //create an in and out connection
   DataOutputStream out = new DataOutputStream(socket.getOutputStream());
   DataInputStream in = new DataInputStream(socket.getInputStream());

   //ask your machine what it's really called
   String hostname = InetAddress.getLocalHost().getHostName();
   println("HOST: "+hostname);

   //converse with the mail server
   readIn(in);
   sendEmail(out, in, "HELO " + hostname + carriageReturn);
   readIn(in);
   sendEmail(out, in, "RSET" + carriageReturn);
   readIn(in);
   //tell the mail server your email
   sendEmail(out, in, "MAIL FROM: " + sender + carriageReturn);
   readIn(in);
   //tell the mail server where you want to send the message
   sendEmail(out, in, "RCPT TO: " + reciever + carriageReturn);
   readIn(in);
   //start the message body
   sendEmail(out, in, "DATA" +  carriageReturn);
   readIn(in);
   sendEmail(out, in, "To:" + reciever + carriageReturn);
   sendEmail(out, in, "From:" + sender + carriageReturn);
   sendEmail(out, in, "Subject: " + subject + carriageReturn);
   sendEmail(out, in, message);

   //close the message body
   sendEmail(out, in, carriageReturn + "." + carriageReturn);
   //end of message body

   //end the conversation
   readIn(in);
   sendEmail(out, in, "QUIT" + carriageReturn);
   readIn(in);

   //close the connections
   in.close();
   out.close();

   println("Message '" + subject + "' sent to '" + reciever + "'");

 }//end of try statement
 catch (UnknownHostException e)
 {
   println("Unknown Host Exception: " + e);
 }//end of catch statement
 catch(IOException e)
 {
   println("Send failure: " + e);
 }//end of catch statement

}//end of method sendMessage

void sendEmail(DataOutputStream out, DataInputStream in, String stringHolder)
throws IOException
{

 if(stringHolder != null)
 {
   out.writeBytes(stringHolder);
   println(stringHolder);
 }//end of if statement

}//end of method sendEmail

void readIn(DataInputStream in)
throws IOException
{
 String record;

 if ((record = in.readLine()) != null)
 {
   println("Message from host: "+record);
 }//end of if statement

}//end of method readIn


Page Index Toggle Pages: 1