Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send emails on Google App Engine

I have tried to use Javamail to send emails. However, I received the following message:

javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Bad Request: ))

I have tried to send emails from the admin account (that I use to upload the app), as well as the user account I login to the app as. (from UserService - getCurrentUser().getEmail()) both failed.

I'm wondering whether there's any special setting I have to setup?

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);    
    Message msg = new MimeMessage(session);
    UserService userService = UserServiceFactory.getUserService();
    String email = userService.getCurrentUser().getEmail();
    //Or
    //String email = "[email protected]";
    msg.setFrom(new InternetAddress(email));
    msg.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("[email protected]"));
    msg.setSubject("Test Email");
    msg.setText("Nobody");
    Transport.send(msg);
like image 323
Roy Chan Avatar asked Jul 12 '09 17:07

Roy Chan


People also ask

How do I send an email from the App Engine?

php require_once 'google/appengine/api/mail/Message. php'; use google\appengine\api\mail\Message; $message_body = 'Hello. This is the body of the message. '; $mail_options = [ 'sender' => '[email protected]', 'to' => '[email protected]', 'subject' => 'Your account has been activated.

Why is my email not sending message?

Maybe you configured your mail client with a wrong outgoing server name: have a look at our list of SMTP and POP providers to double check it, or contact the provider. Firewall or antivirus issues. Make sure that you have an exception rule for your SMTP service in your firewall, proxy service or antivirus settings.

Does Google block port 25?

SMTP relaying through Google Workspace is only allowed through ports 465 or 587. Port 25 is not supported through Google Workspace.


4 Answers

That is really very odd. I just wrote the following sample:

UserService userService = UserServiceFactory.getUserService();
String thisURL = request.getRequestURI();
if (request.getUserPrincipal() != null) {
    response.getWriter().println("<p>Hello, " +
                                request.getUserPrincipal().getName() +
                                "!  You can <a href=\"" +
                                userService.createLogoutURL(thisURL) +
                                "\">sign out</a>.</p>");
    Properties props = new Properties();
    Session mailSession = Session.getDefaultInstance(props, null);    
    Message msg = new MimeMessage(mailSession);
    String email = userService.getCurrentUser().getEmail();
    //Or
    //String email = "[email protected]";
    msg.setFrom(new InternetAddress(email));
    msg.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("[email protected]"));
    msg.setSubject("Test Email");
    msg.setText("Nobody");
    Transport.send(msg);
    response.getWriter().println("<p>Sent email!</p>");
} else {
    response.getWriter().println("<p>Please <a href=\"" +
                                userService.createLoginURL(thisURL) +
                                "\">sign in</a>.</p>");
}

There were no exceptions, and I did receive the email. Are you sure there isn't more going on in the actual application?

like image 69
jsight Avatar answered Oct 25 '22 14:10

jsight


Just scanning the documentation on this I found the following:

For security purposes, the sender address of a message must be the email address of an administrator for the application, or the Google Account email address of the current user who is signed in. The email address can include a "reply to" address, which must also meet these restrictions.

So 'email' should at the very least be set back to your admin emailaccount, or a dedicated emailaccount added as an administrator to your project..

Other than that I see no problems with your code..

like image 22
Tim Avatar answered Oct 25 '22 14:10

Tim


my two cents!! Check if the functionality is part of the server rather than the client classes..

like image 43
srikanth Nutigattu Avatar answered Oct 25 '22 15:10

srikanth Nutigattu


Most probably because you are running your application locally. Upload it to app-engine and it will work fine. *your sender email should be the mail id with which you deploy the project to app-engine* or an admin mail id.

like image 30
Partha Sarathi Avatar answered Oct 25 '22 16:10

Partha Sarathi