views:

1022

answers:

3

Hi Gurus,

I have tired to use Javamail to send email. But I got the following message

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

I have tried to send email from the admin (account I upload the app) or the user I login the app as. (from UserService - getCurrentUser().getEmail()) Both failed

Wonder if there is 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);

Please Advise Thanks Roy

A: 

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

Tim
I tried to use the account that upload the apps and it doesn't work either. Same error message.
Roy Chan
+1  A: 

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?

jsight
I have no idea. But it works now.Thanks jsight =)
Roy Chan
A: 

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

srikanth Nutigattu