0

I want to send a mail from my email address to another email address via clicking a button in JFrame netbeans.Here is the code,

import java.awt.HeadlessException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;
import com.sun.mail.util.MailSSLSocketFactory;
import java.io.File;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.swing.JFileChooser;

    private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
    final String From = txt_from.getText();
    final String password = txt_password.getText();
    String To = txt_to.getText();
    String subject = txt_sub.getText();
    String txtmessage = txt_body.getText();

    Properties props = new Properties();


    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLsocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.fallback", "true");
    props.put("mail.smtp.ssl.socketFactory", "true");
    props.put("mail.smtp.EnableSSL.enable","true");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(From, password);
                }
            }
    );


    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(From));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
        message.setSubject(subject);

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(txtmessage);
        Multipart multiPart = new MimeMultipart();
        multiPart.addBodyPart(messageBodyPart);


        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachment_path);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(txt_DocAttach.getText());
        multiPart.addBodyPart(messageBodyPart);

        message.setContent(multiPart);

        Transport.send(message);
        JOptionPane.showMessageDialog(rootPane, "Message is sent");
    } catch (MessagingException | HeadlessException e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }

}

But it is giving me following error,

javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.io.lOException: Exception in startTLS using SSL socket factory class null: host, port smtp.gmail.com, 587; Exception: java.lang.ClassNotFoundException: javax.netssl.SSLsocketFactory

Tried a lot but can't figure out what to do to solve it? Help please.

Tabassum
  • 25
  • 2
  • 2
  • 8
  • Possible duplicate of [Javamail Could not convert socket to TLS GMail](http://stackoverflow.com/questions/16115453/javamail-could-not-convert-socket-to-tls-gmail) – Michael Mar 31 '17 at 15:19
  • I already use props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); but it's not working. My antivirus is disabled too. – Tabassum Mar 31 '17 at 16:28

3 Answers3

2

Try to change the value of the property mail.smtp.socketFactory.class to javax.net.ssl.SSLSocketFactory instead of javax.net.ssl.SSLsocketFactory, The class name is case sensitive.

For more infromations about connection properties you can look at: Where to find all available Java mail properties?

Ron Badur
  • 1,873
  • 2
  • 15
  • 34
  • Error- javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://supporlgoogle.comimail/answer/78754 t6sm11250727pgo.42 - gsmtp – Tabassum Mar 31 '17 at 16:47
  • this is a diffrenet problem maybe the answer in this link can help you http://stackoverflow.com/questions/20337040/gmail-smtp-debug-error-please-log-in-via-your-web-browser I recommend to you to open new question for this if the above link won't help you – Ron Badur Mar 31 '17 at 16:56
  • glad to hear, accept the answer for the future people looking at this question :) – Ron Badur Mar 31 '17 at 17:09
1

As others have pointed out, your socket factory settings were wrong.

More importantly, you never needed to set them at all!

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

This error can also pop up if your JavaMail lib (mail.jar or javax.mail.jar) is too old. Download the newest version from here: https://javaee.github.io/javamail/

SJX
  • 1,071
  • 14
  • 15