How to send an email via Processing?

edited June 2016 in How To...

Hello,

I've found this topic: https://processing.org/discourse/beta/num_1204669725.html for sending emails (example of Daniel Swiffmann).

If I use the code, I get a mail of Google which sais:

Sign-in attempt blocked

Someone just tried to log into your Google account ******@gmail.com from an application that does not meet modern safety standards.

Is there a code which does work in Processing 3?

Thanks, Daantje

Answers

  • Answer ✓

    I think Google has to give permission to apps to access email. Like, if you setup outlook with your google Mail account, it brings you too a page which says "Do you want to allow this app to access your data?" And stuff like that. So I think it is google that is not working with the app. If you have an email from a different provider, try that? Hope this helps. :)

  • Answer ✓

    Hey @Repet you're right on with the suggestion, is it fine if I do a little touch up to that answer (just because I did this really recently) so @Daantje what @Repet said is totally correct, now what you're going to do is sign in to your Gmail account, then afterwards just search on google, "less secure apps third party" and you will see one of the first ones as your answer, it will literally say "Less secure apps - Google" or something like that. Or you can just go to your account, go to Settings, then Security, then Less secure Apps. All you have to do is turn off that security feature. GMail will freak out that you want to do it, but just say that You're sure and let it work it's magic. Yeah it can be a very frustrating problem indeed, and I found this answer by accident, it took me such a long time! If you need any help with JavaMail be sure to message me (because again I did a pretty big project with it recently) and I'll do my best to help.

  • edited June 2016

    @Repet, thanks for your answer. I only have an email account at Gmail, no Outlook or so.

    @TechWiz777, thanks for your help. I am going to try your answer, hope it will work... :D

    Regards, Daantje

    ---------- ---------- Edit: ---------- ----------

    Thank you so much! I almost gave it up, but it works very good now! Wow, I was searching for this problem for a few weeks, but I didn't thought it was so 'simple' (just accept it in my Google account)...

    Thanks! :-)

  • @Daantje Could you post a working sample? Did you use the javax/mail lib? I am interested in this as well.

    Kf

  • edited June 2016

    @kfrajer, of course:

    First tab (name: save_attachment.pde)

    // 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.*;
    
    void setup() {
      size(200,200);
    
      sendMail();
      println("Finished");
      noLoop();
    }
    

    Second tab (name: Auth.pde)

    // 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 = "someone@example.com";
        password = "myPassword";
        System.out.println("authenticating... ");
        return new PasswordAuthentication(username, password);
      }
    }
    

    Third tab (name: MailStuff.pde)

    // Example functions that send mail (smtp)
    // You can also do imap, but that's not included here
    
    // A function to check a mail account
    import java.util.*;
    import java.io.*;
    import javax.activation.*;
    
    // A function to send mail
    void sendMail() {
      // 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());
    
      try
      {
        MimeMessage msg=new MimeMessage(session);
        msg.setFrom(new InternetAddress("someone@example.com", "Name"));
        msg.addRecipient(Message.RecipientType.TO,new InternetAddress("recipient@example.com"));
        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("c:\\image.jpg");
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("image.jpg");
        multipart.addBodyPart(messageBodyPart);
        msg.setContent(multipart);
        msg.setSentDate(new Date());
        Transport.send(msg);
        println("Mail sent!");
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    
    }
    

    Downloads

    You also have to download the JavaMail library: http://www.oracle.com/technetwork/java/javamail/index-138643.html

    If you download it, you will get a folder with lots of files. Only the .JAR file named 'mail.jar' is necessary. In Processing under the 'Sketch' tab, you can select 'Add file' or something like that. You will get a window to select the downloaded 'mail.jar' file.

    Last but not least

    You also have to do the things @TechWiz777 said: You have to allow "yourself" in your own Gmail account to use less secure apps. For a discription about this, you can use the page from Google itself: https://support.google.com/accounts/answer/6010255?hl=en

    If you have any questions, ask them and I will help you.

    Regards, Daantje

  • Thanks for posting @Daantje. I will try it out tonight!

    Kf

  • Fancy code :)

Sign In or Register to comment.