Reputation: 824
Am writing the code as follow in my application
public void send_email(String email)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sendgrid.net");
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("my_username","my_password");
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is method is called in my servlet class.
After that it executed well and give a message as "Done
" on console , but i didnt receive any email in Email-inbox.
If i run this same code as java application it works fine and received an email.
But when i run it on Google web server its not working.. And one thing, here i removed both javaee.jar file and mail.jar files from lib, but still it didn't give any error..
Give me any suggestions guys....
Upvotes: 0
Views: 169
Reputation: 6486
Be aware you cannot send an email from just any email address. You need to use one that is authorized in your app's domain.
The email address of the sender, the From address. The sender address must be one of the following types:
The address of a registered administrator for the application. You can add administrators to an application using the Administration Console.
The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a Gmail account, or be on a domain managed by Google Apps.
Any valid email receiving address for the app (such as [email protected]).
Any valid email receiving address of a domain account, such as [email protected]. Domain accounts are accounts outside of the Google domain with email addresses that do not end in @gmail.com or @APP-ID.appspotmail.com.
https://developers.google.com/appengine/docs/python/mail/sendingmail
Upvotes: 0
Reputation: 23535
But when i run it on Google web server its not working..
"Google web server" in your case means Google AppEngine? If so then you cannot use the full JavaMail API but must use Google's infrastructure.
An app cannot use the JavaMail interface to connect to other mail services for sending or receiving email messages. SMTP configuration added to the Transport or Session is ignored.
Upvotes: 1