Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...