Sending email using Javamail - jakarta-mail

I'm trying to send a mail using the GoDaddy email host I have registered couple of days ago using Java mail api, however it turns out that its not that easy to implement,
and I am getting, this error:
Could not connect to SMTP host: smtpout.asia.secureserver.net, port: 80, response: -1
I have tried ports 3535, 465, 587, 25 but still get the same error. The same code below has been tested to work with sending out email using Gmail, with the addition of this code (which I have omitted in this case):
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
MailSender.java:
public class MailSender {
private static String HOST = "smtpout.asia.secureserver.net";
private static String PORT = "80";
public static void sendMail(final Mail mail) throws MailException {
EmailValidator validtor = new EmailValidator();
if (validtor.validate(mail.getReceipient())) {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", PORT);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mail.getUsername(),mail.getPassword());
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mail.getSender()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(mail.getReceipient()));
message.setSubject(mail.getSubject());
message.setText(mail.getBody());
Transport.send(message);
System.out.println("OK");
} catch (MessagingException e) {
throw new MailException(e.getMessage());
}
} else {
throw new MailException("Email address not valid.");
}
}
}
The Mail parameter in this class holds all other mail information, the username/password, sender and recipient email address string, which is tested to work with email clients like Outlook & Thunderbird.

Port 80 is used for HTTP.
Change it to 465 or 587.
(Consult the GoDaddy documentation for the correct port)

Apparently, the problem was not with Java mail api, but was with GoDaddy server, I have consulted their tech support and work fine now.

Related

Java mail API works on localhost bu not on google app engine

I have a sending email logic using Java Mail API and it works fine when I am on localhost but once deployed on google cloud platform, the email goes to my servlet but never gets delivered.
I bought the email from GoDaddy so it's: xxx#mydomain.com.
After reading docs on google cloud platform and some comments here on StackOverflow, I have configured firewall rules but nothing seems to work to allow ingress and egress on port 25, 465 and 587 (I know google doesn't allow traffic on port 25).
I don't wanna use 3rd party email senders like sendbird... because I was using elastic before and I didn't need a 3rd party email sender, JavaMail was enough.
So I think Java mail should be enough for GCP.
Can anyone help me out?
Here is my sending email logic
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* #author sidibe ibrahim
* Sending email logic
*/
public class EmailSender {
static MessagingException me;
public static boolean sendMail(String from, String password, String message, String to[], String title) throws UnsupportedEncodingException {
String host = "smtpout.secureserver.net";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.host", 465);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.trust", "*");
Session session = Session.getDefaultInstance(props, null);
MimeMessage mimeMessage = new MimeMessage(session);
try {
mimeMessage.setFrom(new InternetAddress(from, "xxx"));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
//sdd subject
mimeMessage.setSubject(title);
//set message to mimeMessage
mimeMessage.setText(message, "UTF-8", "html");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException m) {
me.printStackTrace();
}
return false;
}
}
The App Engine Mail API (which also supports JavaMail) has already been deprecated.
Instead, GCP recommends to use a third-party mail provider such as:
SendGrid
Mailgun
Mailjet
EDIT
If you would still like to continue with the outdated solution, however, see the article on sending emails with the Mail API.

Java Mail Delegation Access not working for Office 365

If you see the commented line of code store.connect.... I am trying to access the shared mailbox through a usermail box [migrated to cloud] which has a delegate access on this shared mailbox. On running this I get
javax.mail.AuthenticationFailedException: LOGIN failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:715)
at javax.mail.Service.connect(Service.java:364)
at com.adecco.smpt.TestSSL.main(TestSSL.java:26)
If I try to access user-mail box instead of shared mailbox everything works well. Is there a different syntax to access the SHARED MAILBOX in JAVA ?? Thanks in advance.
import java.util.Properties;
import javax.mail.*;
public class TestSSL {
public static void main(String[] args) {
Properties props = System.getProperties();
props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.smtp.ssl.enable", "true");
try {
Session session = Session.getInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imaps");
store.connect("outlook.office365.com", 993, "office 365 username/alias name of shared MAILBOX", "PASSWORD");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for (Message message : messages) {
System.out.println(message);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
}
First, fix these common mistakes.
It looks like this article has the information you need.

Enforcing Basic Authentication with RestEasy & TJWS

We use Resteasy to communicate between multiple backend servers & we want to lock this down so not just anyone can attach a client or browser to the restlet server.
We're using Resteasy 3.04 and as our backend services are numerous but very light-weight an embeddded TJWS webserver.
Example Server code:
public class RestEasySSLBasicAuthenticationServer {
static TJWSEmbeddedJaxrsServer webServer;
static class BasicAthenticationSecurityDomain implements SecurityDomain {
#Override
public Principal authenticate(String aUsername, String aPassword) throws SecurityException {
System.out.println("User:" + aUsername + " Password" + aPassword);
if (aPassword.equals("password") == false) {
throw new SecurityException("Access denied to user " + aUsername);
}
return null;
}
#Override
public boolean isUserInRoll(Principal aUsername, String aRole) {
// No role based checks so return true
return true;
}
}
public static void main(String[] args) throws Exception {
// Create embedded TJWS web server
webServer = new TJWSEmbeddedJaxrsServer();
// Set up SSL connections on server
webServer.setSSLPort(8081);
webServer.setSSLKeyStoreFile("K:\\source\\RestEasyTest\\server_localhost.jks");
webServer.setSSLKeyStorePass("krypton");
webServer.setSSLKeyStoreType("JKS");
// Add basic HTTP authentication to the server
webServer.setSecurityDomain( new BasicAthenticationSecurityDomain() );
// Add the restlet resource
webServer.getDeployment().getActualResourceClasses().add(PlayerResource.class);
// Start the web server
webServer.start();
// Run until user presses a key
System.out.print("Web server started. Press a key to stop...");
System.in.read();
// Stop the web server
webServer.stop();
}
}
Example client code:
public class RestEasySSLBasicAuthenticationClient {
public static void main(String[] args) throws Exception {
// Set up the keystore
System.setProperty("javax.net.ssl.keyStore", "K:\\source\\RestEasyTest\\client_localhost.jks");
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStorePassword", "krypton");
// Create a new Restlet client
Client restletClient = ClientBuilder.newClient();
// *** Even WITHOUT these credentitials we can access the restlet
// restletClient.register(new BasicAuthentication("username", "password"));
// Set up the restlet request target.
WebTarget request = restletClient.target("https://localhost:8081/player/{id}");
request = request.resolveTemplate("id", Long.valueOf(1));
// Build the restlet request
Invocation invocation = request.request("application/xml").buildGet();
// Call the restlet and get returned object
Player result = invocation.invoke( Player.class );
System.out.println(result.toString());
}
}
Using the test client and a registered authentication filter works and as expected I can a 401 access error if I get the password incorrect.
However if no authentication is registered at the client then the server never calls the SecurityDomain check and access is allowed.
How do I enforce a login at the server?
You can ensure all users are authenticated by enabling security on the embedded TJWS web server.
webServer.getDeployment().setSecurityEnabled(true);

MessagingException thrown on some machines, but on others it works fine

I'm trying to write a method that sends an email based on parameters, and it works completely on CentOS and OSX. However, the method doesn't work properly on Windows (even when recompiled on Windows) as well as some other Linux OS's - it throws a MessagingException. Does anyone have any ideas as to how I can fix this to work on Windows? Thanks!
private static void sendEmail(String towhom, String subject, String body) {
String host = "smtp.gmail.com", from = "myemail", pass = "mypassword";
Properties props = System.getProperties();
Scanner scan = new Scanner(System.in);
try {
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress toAddress = new InternetAddress(towhom);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(AddressException e) {
System.out.println("Invalid Email Address.");
}
catch(MessagingException e) {
System.out.print("\nInvalid Email Address, please reenter it: ");
sendEmail(scan.nextLine(), subject, body);
}
}
So yes Avast Antivirus was causing the exception to be thrown when I was trying to send the mail. If anyone else has this problem who finds this page:
Open up Avast and click on the Security tab. Then click on the AntiVirus tab on the left. Under that, click Mail Shield and go to the settings. Untick "Scan outbound mail (SMTP)" and it will work like a charm.
There is no problem with your code. It looks good.
According to the JavaMail FAQ's. Following could be the problem -
There's a firewall or anti-virus program intercepting your request.
There's something wrong in your JDK installation preventing it from finding the certificates for the trusted certificate authorities.
You're running in an application server that has overridden the JDK's list of trusted certificate authorities.
If disabling your firewall and/or anti-virus does not solve the problem then you can try and reinstall the JDK and test.

email address on GAE

I have an application on google app engine like abc.appspot.com can I have an email address to send/receive emails like admin#abc.appspot.com kindly help me.
Edit
here is my SendMail class
public class SendMail {
private static String fromAddress = "abc#gmail.com";
private static Logger log = Logger.getLogger(SendMail.class.getCanonicalName());
// Send the Mail
public void send(String toAddress, String subject, String msgBody)
throws IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddress));
InternetAddress to = new InternetAddress(toAddress);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setText(msgBody);
Transport.send(msg, new InternetAddress[] { to });
} catch (AddressException addressException) {
log.log(Level.SEVERE, "Address Exception , mail could not be sent", addressException);
} catch (MessagingException messageException) {
log.log(Level.SEVERE, "Messaging Exception , mail could not be sent", messageException);
}
}
}
So it sends an email regarding abc#gmail.com but I want that it should send from email#abc.appspot.com.
You can only receive emails in the form of #abc.appspotmail.com. AFAIK there is no way to have #abc.appspot.com as receiving address.
If you wan to receive emails from your custom domain, e.g. #abc.com, than the only way is to have external email service forward emails to your #abc.appspotmail.com. Most domain registrars offer free limited email service with forwarding (we use GoDaddy and get limited forwarding free).
Yes you can: https://developers.google.com/appengine/docs/java/mail/usingjavamail#Senders_and_Recipients

Resources