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 & HelpOther Libraries › Processing to send e-mail or SMS
Page Index Toggle Pages: 1
Processing to send e-mail or SMS? (Read 2875 times)
Processing to send e-mail or SMS?
Nov 26th, 2007, 10:33am
 
I'm trying to get Processing to either send an SMS or send an e-mail after a certain event occurs. Is there a library for this (specifically the e-mail sending)?

Any suggestions for what library to use for e-mail sending? I only need it to send a simple e-mail from one e-mail address to another with a subject and a line of text in the body.

can I use the Commons Email and JavaMail libraries with Processing? If so...do I put all the .java files in my library folder in Processing? Do I include them the same way I include Processing libraries?

Thanks,
aylin
Re: Processing to send e-mail or SMS?
Reply #1 - Nov 26th, 2007, 10:58am
 
http://www.shiffman.net/2007/11/13/e-mail-processing/
Re: Processing to send e-mail or SMS?
Reply #2 - Nov 30th, 2007, 11:47pm
 
Thank you so much for passing that along. It's exactly what I was looking for!
Re: Processing to send e-mail or SMS?
Reply #3 - Jan 20th, 2010, 7:37am
 
That's really great!!

But I'm wondering about this:
From the Processing IDE, the program works perfectly, but when I export the code to an exe file, it will not send a mail.

Does someone have an idea what could be the reason?

Regards,

Roland
Re: Processing to send e-mail or SMS?
Reply #4 - Jan 20th, 2010, 3:07pm
 
Where is JavaMail library when you export the code?
Re: Processing to send e-mail or SMS?
Reply #5 - Jan 21st, 2010, 2:48am
 
Hi PhiLho,

I've got this structure under application.windows:

Email.exe
lib
    |-args.txt
    |-core.jar
    |-Email.jar
    |-smtp.jar
source
    |-Email.java
    |-Email.pde
    |-Auth.pde
    |-MailStuff.pde

Something's missing?

Regards,

Roland
Re: Processing to send e-mail or SMS?
Reply #6 - Jan 21st, 2010, 10:33am
 
Ah, I didn't noticed the Email.zip link.
So, I downloaded it, put it in the sketchbook, and edited it to adapt to my test e-mail account. I had some trouble to make it work with an authenticated SMTP server, but finally it worked.
I exported the sketch to a Windows application, but when I ran it, I got nothing, too.

I added a logger to replace the System.out.println() calls, so I get the results to a file, since it seems we cannot get stdout output from an exported Processing application.
And, well, after that, it worked! Strange.. Unless the printlns cause some failure/exception or something.

Mmm, I added a method to the logger to log exceptions, and it appears POP3 access doesn't work in application form while it is OK in the PDE. Strange... But I do receive the messages sent.
Re: Processing to send e-mail or SMS?
Reply #7 - Jan 22nd, 2010, 12:49am
 
But there must be some other problem...

I only need the capability to send mails, so I've deleted the checkMail() function from MailStuff module.
I've also comented out the lines
"    println("Mail sent!");"
and
"    //e.printStackTrace();"
in MailStuff.

But it will not send mails   Cry

Can you give me a hint how I can get the error message on the screen?

Regards,
Roland


Here is the complete code:
Module Email:
// Daniel Shiffman              
// http://www.shiffman.net      

// Simple E-mail Checking        
// This code requires the Java mail library
// smtp.jar, pop3.jar, mailapi.jar, imap.jar, activation.jar
// Download:// http://java.sun.com/products/javamail/

import javax.mail.*;
import javax.mail.internet.*;

int success = 0;

void setup() {

 PFont font = loadFont("ArialUnicodeMS-12.vlw");  
 textAlign(CENTER);
 textFont(font);
 // Function to send mail
 sendMail();
 
   size(200,200);
   if (success == 0) {
     text("Sent mail",100,40);
   } else {
     text("Error",100,40);
   }
 //exit();
 noLoop();
}

Module Auth:
// Daniel Shiffman              
// http://www.shiffman.net      

// Simple Authenticator          
// Careful, this is terribly unsecure!!

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Auth extends Authenticator {

 public Auth() {
   super();
 }

 public PasswordAuthentication getPasswordAuthentication() {
   String username, password;
   username = "<USER>";
   password = "<PASSWORD>";
   //System.out.println("authenticating. . ");
   return new PasswordAuthentication(username, password);
 }
}

Module MailStuff:
// Daniel Shiffman              
// http://www.shiffman.net      

// Example functions that check mail (pop3) and send mail (smtp)
// You can also do imap, but that's not included he
// A function to send mail
void sendMail() {
 // Create a session
 String host="exchg02";
 Properties props=new Properties();

 // SMTP Session
 props.put("mail.transport.protocol", "smtp");
 props.put("mail.smtp.host", host);
 props.put("mail.smtp.port", "25");
 props.put("mail.smtp.auth", "true");
 // We need TTLS, which gmail requires
 props.put("mail.smtp.starttls.enable","true");

 // Create a session
 Session session = Session.getDefaultInstance(props, new Auth());

 try
 {
   // Make a new message
   MimeMessage message = new MimeMessage(session);

   // Who is this message from
   message.setFrom(new InternetAddress("admin@domain.com", "Name"));

   // Who is this message to (we could do fancier things like make a list or add CC's)
   message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("<Recipient@domain.com", false));
   // Subject and body
   message.setSubject("Processing ist prima");
   message.setText("It's great to be here. . .");

   // We can do more here, set the date, the headers, etc.
   Transport.send(message);
   //println("Mail sent!");
   success = 0;
 }
 catch(Exception e)
 {
   //e.printStackTrace();
   success = 1;
 }
}
Re: Processing to send e-mail or SMS?
Reply #8 - Jan 22nd, 2010, 1:08pm
 
Here is my Logger.pde: Code:
class Logger
{
String m_fileName;

Logger(String fileName)
{
m_fileName = fileName;
}

void log(String line)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(m_fileName, true));
pw.println(line);
}
catch (IOException e)
{
e.printStackTrace(); // Dumb and primitive exception handling...
}
finally
{
if (pw != null)
{
pw.close();
}
}
}

void log(String[] lines)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(m_fileName, true));
for (int i = 0; i < lines.length; i++)
{
pw.println(lines[i]);
}
}
catch (IOException e)
{
e.printStackTrace(); // Dumb and primitive exception handling...
}
finally
{
if (pw != null)
{
pw.close();
}
}
}

void log(String errorMessage, StackTraceElement[] ste)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(m_fileName, true));
pw.println(errorMessage);
for (int i = 0; i < ste.length; i++)
{
pw.println("\tat " + ste[i].getClassName() + "." + ste[i].getMethodName() +
"(" + ste[i].getFileName() + ":" + ste[i].getLineNumber() + ")"
);
}
}
catch (IOException e)
{
e.printStackTrace(); // Dumb and primitive exception handling...
}
finally
{
if (pw != null)
{
pw.close();
}
}
}
}
Changed code in MailStuff.pde like: Code:
void checkMail() {
Logger logger = new Logger(sketchPath("POP3.txt"));
// [...]
// Create authentication object
logger.log("Authenticating...");
Auth auth = new Auth();
// [...]
logger.log("Connecting...");
Session session = Session.getDefaultInstance(props, auth);
// [...]
folder.open(Folder.READ_ONLY);
logger.log(folder.getMessageCount() + " total messages");

// Get array of messages and display them
Message message[] = folder.getMessages();
for (int i = 0; i < message.length; i++) {
String content = message[i].getContent().toString();
logger.log(new String[]
{
"---------------------",
"Message # " + (i+1),
"From: " + message[i].getFrom()[0],
"Subject: " + message[i].getSubject(),
"Message:",
content
});
}

// Close the session
folder.close(false);
store.close();
}
// This error handling isn't very good
catch (Exception e) {
logger.log(e.toString(), e.getStackTrace());
}
}

// A function to send mail
void sendMail() {
Logger logger = new Logger(sketchPath("SMTP.txt"));
// [...]
logger.log("Connecting...");
Transport tr = session.getTransport("smtp");
tr.connect(host, 587, "user%example.com", "smartpassword");
message.saveChanges();

logger.log("Sending message");
tr.sendMessage(message, message.getAllRecipients());
tr.close();

logger.log("Mail sent!");
}
catch(Exception e)
{
logger.log(e.toString(), e.getStackTrace());
}
}

Note: one of my e-mail providers either require a POP3 access before sending SMTP messages, or doing authenticated SMTP access: by default, SMTP is free to use by everybody, but since spammers abused the feature, providers now avoid "open relays" and are more restrictive.
Re: Processing to send e-mail or SMS?
Reply #9 - Jan 25th, 2010, 4:17am
 
Hi PhiLho,

thank you for the hint...

I've used the logger class and found this message in the SMTP.txt file:

"...no object DCH for MIME type text/plain..."

With this information, I found that link

http://www.jguru.com/faq/view.jsp?EID=237257

Near the middle of the page, there is a friend named Austin, who shows the solution.

I added the proposed lines of code into the mailStuff module and now it works fine... Smiley

Thank you for your help!

Roland
Re: Processing to send e-mail or SMS?
Reply #10 - Jan 25th, 2010, 5:11am
 
Thanks for the info. For the record, the solution was to add:
Code:
// add handlers for main MIME types
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);

to the e-mail configuration code (Java 1.6).

I am glad my little class was useful! Smiley
Page Index Toggle Pages: 1