Trying to send mail using smtp, used all the necessary jar files(mail.jar and activation.jar) but always shows this error
error shown on running testmail file
I have used two classes, one is to send the email and another one calls the function in that file
Code for sendmail.java
import javax.mail.*;
import javax.mail.internet.*;
class SendMail{
public static void send(String from,String password,String to,String sub,String msg){
//Get properties object
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.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
Code for testmail.java
public class testmail{
public static void main(String[] args) {
//from,password,to,subject,message
SendMail.send("myemail","mypassword","reciever","hello javatpoint","How r u?");
System.out.println("message sent successfully");
//change from, password and to
}
}
both the files are compiling but when I execute the testmail file the error is shown
I have used the apache tomcat 9.0 server
plz check your version of javax.mail
, i am using 1.6.2
, there is no problem to run your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.