简体   繁体   中英

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 = "my_admin_account@gmail.com";
    msg.setFrom(new InternetAddress(email));
    msg.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("some_test_email@gmail.com"));
    msg.setSubject("Test Email");
    msg.setText("Nobody");
    Transport.send(msg);

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 = "my_admin_account@gmail.com";
    msg.setFrom(new InternetAddress(email));
    msg.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("jesse.sightler@gmail.com"));
    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?

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..

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

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.

Make sure you are using the message from an email address corresponding to the currently logged-in user or the email of the account where the applications is deployed.

The most important thing is that messages are not sent if the application is run locally. To actually send the messages, deploy it into Google App Engine and run it remotely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM