Based on Daniel Shiffman’s Email code, I’ve modified it and changed it to a multithreaded version for sending email in Processing Sketch.
This example allows you to click and add an email in the stack and you can attach a file with it.
You will need to include a folder called “Code” with smtp.jar, pop3.jar, mailapi.jar, imap.jar and activation.jar in your sketch folder. This could be downloaded from Dan’s Example as well.
http://www.shiffman.net/2007/11/13/e-mail-processing/
Email_multithreaded.pde // Daniel Shiffman // http://www.shiffman.net // Example functions that send mail (smtp) // You can also do imap, but that's not included here // A function to check a mail account //Multithread version modified by Hamlet Lin@2012 import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; Thread tt; EmailSender emailSender; String printFolder; PFont sysFont; void setup(){ printFolder=dataPath("")+"\\"; emailSender=new EmailSender(); tt=new Thread(emailSender); tt.start(); size(400, 400); sysFont=createFont("arial", 12); textFont(sysFont, 12); } void draw(){ fill(255); textAlign(CENTER); textFont(sysFont); background (color(frameCount%255, frameCount%512, frameCount%786)); text(emailSender.sendingEmail+"", width/2, height/2); } void mouseReleased(){ emailSender.addMail("asad@example.com", "fileNameInDataPath"); } class EmailSender implements Runnable { ArrayList emailAddresses; ArrayList fileNames; boolean sendingEmail=false; public EmailSender() { emailAddresses=new ArrayList(); fileNames=new ArrayList(); } void run(){ while (1!=0){ //println("mail left: "+emailLeft); if (emailAddresses.size()>0){ sendingEmail=true; //println((String)emailAddresses.get(0)); sendMail((String)emailAddresses.get(0), (String)fileNames.get(0)); emailAddresses.remove(0); fileNames.remove(0); }else{ sendingEmail=false; } } } void addMail(){ emailAddresses.add(""); fileNames.add(""); // println("emailLeft: "+emailLeft); } void addMail(String _reciever, String _filename){ emailAddresses.add(_reciever); fileNames.add(_filename); // println("emailLeft: "+emailLeft); } // A function to send mail void sendMail(String _reciever, String _fileName) { // Create a session String host="smtp.gmail.com"; Properties props=new Properties(); // SMTP Session props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); 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()); DataSource source = new FileDataSource(printFolder+_fileName); try { MimeMessage msg=new MimeMessage(session); msg.setFrom(new InternetAddress("", "")); msg.addRecipient(Message.RecipientType.BCC,new InternetAddress(_reciever)); msg.setSubject("Email with Processing"); BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("Email sent with Processing"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); // DataSource source = new FileDataSource(dataPath(_fileName)); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(_fileName); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); msg.setSentDate(new Date()); Transport.send(msg); println("Mail sent!"); } catch(Exception e) { e.printStackTrace(); } } } void stop() { super.stop(); }
Auth.pde
// 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 = "asfaf@gmail.com"; password = "password"; System.out.println("authenticating. . "); return new PasswordAuthentication(username, password); } }
Pingback: Arduino: MF522-AN RFID reader »