1

In googel app engine, if we are not using OpenID login, we can send mails like its written in API

But I use OpenId Login (using Google mail) and I cant use this.

But I do something like thatL

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                "mail@gmail.com", "pass");
                    }
                });



            **Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("mail"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("mail"));
            message.setSubject("Testing Subject");
            message.setText(msgBody);
            Transport.send(message);

IF I WILL LOG IN AND THEN CALL THAT SERVLET, for instance www.example.appspot.com/mail it works! but if I'm not logged in, it does not work! But I don't understand what happens?!**

java.lang.RuntimeException: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender))
    at test.queue.MailServlet.sendMail(MailServlet.java:208)

at this 208 line I have this:

protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("example@gmail.com","pass");
        }
grep
  • 5,465
  • 12
  • 60
  • 112
  • I need something, without auth – grep Oct 09 '13 at 16:59
  • There is no way you can send email in the name of other users without authorisation. – Peter Knego Oct 09 '13 at 19:41
  • I don't understand what do you mean.. can you tell me this more clearly? I have emails in database (its app engine application). and want to send mails all the users... – grep Oct 09 '13 at 19:44
  • Yes you can send emails, but the question is who you want to be in the `from:` field? – Peter Knego Oct 09 '13 at 19:48
  • I don't care who will be in from field (It is not important for me) I just want to send emails. I have cron schedule. It gets user emails from the db. then I want to send mails to each user. P.S I have free user. – grep Oct 09 '13 at 19:52
  • the problem is that, I run rcon schedule, I'm not logged in, when my service tries to call mail sender servlet. – grep Oct 09 '13 at 19:54

1 Answers1

2

the address set in the "from" should be present under the Admission -> Permissions for the gae application, and for sending email from this permitted account, no need to specify password in the code.

"For security purposes, the sender address of a message must be the email address of an administrator for the application or any valid email receiving address for the app (see Receiving Mail). The sender can also be the Google Account email address of the current user who is signed in, if the user's account is a Gmail account or is on a domain managed by Google Apps." - as mentioned here https://developers.google.com/appengine/docs/java/mail/#Java_Sending_mail_with_the_JavaMail_API

Devashish Mamgain
  • 2,077
  • 1
  • 18
  • 39
  • If I set my administrator mail, will that code work every time in any situation? – grep Oct 09 '13 at 21:49
  • yes @DSTH it will work in all the cases, no need to give the password in the code. just this code will work, we are using it for mobitexter.net welcome email on user registration, it works without any problem. Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("user@example.com", "Mr. User")); msg.setSubject("Your Example.com account has been activated"); msg.setText(msgBody); – Devashish Mamgain Oct 10 '13 at 08:16
  • can u add few log statements before and after the mail sending code, also pls check ur spam folder. – Devashish Mamgain Oct 10 '13 at 08:41
  • its INFO. There is nothing :( – grep Oct 10 '13 at 08:47
  • plase wait. I will check my LOG AGAIN. – grep Oct 10 '13 at 08:54
  • https://developers.google.com/appengine/docs/java/mail/#Java_Sending_mail_with_the_JavaMail_API – Devashish Mamgain Oct 10 '13 at 08:55
  • see my question here: http://stackoverflow.com/questions/19291429/app-engine-mail-is-not-sending – grep Oct 10 '13 at 09:02
  • yes I did not have OH, I did not have Transport.send(message); – grep Oct 10 '13 at 09:06