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);
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.
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.
SMTP relaying through Google Workspace is only allowed through ports 465 or 587. Port 25 is not supported through Google Workspace.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With