Sending mail from your computer using java, what do you need?
I have a windows 7 system with no smtp servers installed, only java. I want to send mail from my program via gmail. I have written a java program to google connect via smtp and have telnet option enabled, but I am getting below error. I've tried using ports 465 and 587, but no change.
Mistake:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at mail1.SendMailSSL.main(SendMailSSL.java:44)
Code:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mymail@gmail.com","mypassword");
}
} );
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("mymail@gmail.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"to24n@gmail.com"));
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 (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Can anyone tell me what I need to do to send an email from my personal system? Please tell me step by step and include something like software installation because I have to do it for my project. Your answer is greatly appreciated. Thank you in advance.
source to share
Two years ago I had the same problem, but I don't remember everything that helped me.
- I have used JavaMail.
- In some cases you need to use 587, in some cases you need to use port 465.
- You have to allow SMTP (or POP?) In your gmail settings. You must use authorization.
I think these posts will be helpful:
source to share
Required jar files:
- MAIL.JAVR
- JAVAEE.JAR
- activation.jar
- DNSJAVA.JAR
- SENDAMAIL-TOMCAT-2.1.2.JAR
Steps to send mail:
-
Use google smtp server port
465
-
Enable telnet on Windows 7:
Control Panel > Programs > Turn Windows features on or off
Check "Telnet Server" and "Telnet Client"
-
Make sure your system has an accurate date
- And, of course, an internet connection is required.
source to share