Search code examples
javaemailjakarta-mailparseexception

javax.mail.internet.ParseException: In Content-Type string <text>, expected '/', got null


I know this question was answered somewhere else but that didn't work for me so here i am.

Basically my code will login to your e-mail and send messages to multiple people.

of course its not done, i know that, but it has enough code to work because i based it off one of my command line programs that does the same thing.

anyways here is some code.

sender class :

public class Sender {

public static void send(JTextArea listRecepients, JTextField textSubject, JTextArea textBody, JTextField txtSMTP,
        JTextField txtEmail, JPasswordField txtPassword, JCheckBox boxHtml, JSpinner ammountSpin, JSpinner timeSpin, JProgressBar progressBar) {

    String subject = textSubject.getText();

    String message = textBody.getText();

    for (String line : listRecepients.getText().split("\\n")) setEmails(line, subject, message, txtSMTP, txtEmail, txtPassword, boxHtml, ammountSpin, timeSpin, progressBar);
}

private static void setEmails(String line, String subject, String message, JTextField txtSMTP,
        JTextField txtEmail, JTextField txtPassword, JCheckBox boxHtml, JSpinner ammountSpin, JSpinner timeSpin, JProgressBar progressBar) {

    List<String> emails = new ArrayList<String>(Arrays.asList(line));
    sendEmail(subject, emails, message, txtSMTP, txtEmail, txtPassword, boxHtml, ammountSpin, timeSpin, progressBar);
}

public static void sendEmail(final String subject, final List<String> emailToAddresses,
            final String emailBodyText, JTextField txtSMTP, JTextField txtEmail, JTextField txtPassword, JCheckBox boxHtml, JSpinner ammountSpin, JSpinner timeSpin, JProgressBar progressBar) {

    final String username = txtEmail.getText();

    final String password = txtPassword.getText();

    final String smtpHost = txtSMTP.getText();

    Properties props = new Properties();

    // do not change - start
    props.put("mail.smtp.user", "username");
    props.put("mail.smtp.host", smtpHost);
    // props.put("mail.debug", "true");
    props.put("mail.smtp.auth", "true");
    // do not change - end

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
    String emails = null;
    int ammount, time;

    try {
        ammountSpin.commitEdit();
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ammount = (int) ammountSpin.getValue();

    try {
        timeSpin.commitEdit();
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    time = (int) timeSpin.getValue();
    time = time*1000;
    try {

        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress(username));

        message.setSubject(subject);

        if (boxHtml.isSelected() == true){
            String content = "<html>\n<body>\n";
            content += emailBodyText + "\n";
            content += "\n";
            content += "</body>\n</html>";
            message.setContent(content, "html");
        }else{
            String content = emailBodyText;
            message.setContent(content, "text");
        }

        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String email : emailToAddresses) {
            sb.append(email);
            i++;
            if (emailToAddresses.size() > i) {
                sb.append(", ");
            }
        }

        emails = sb.toString();

        message.setRecipients(Message.RecipientType.BCC,
                InternetAddress.parse(sb.toString()));

        System.out.println("Sending Email to " + emails + " from "
                + username + " with Subject - " + subject);

        for (int x = 0; x < ammount; x++){
            Transport.send(message);
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        System.out.println("Email successfully sent to " + emails);

    } catch (MessagingException e) {
        System.out.println("Email sending failed to " + emails);
        failed(e);
    }
}
public static void failed (MessagingException e){
    JFrame frame = new JFrame("Failed to send");
    JOptionPane.showMessageDialog(frame, "The Process has failed with exception code : "+ e, "Warning", JOptionPane.WARNING_MESSAGE);
}   

}

and yes i know there are more efficient ways of doing this insted of sending the required fields through multiple methods but im lazy. thats why i came here :p

thanks for your help and lmk if you want the other 3 classes. but the error should be in this one.


Solution

  • I figured it out, basically i changes the props.put values to this :

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.user", username);
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.port", smtpPort);
    props.put("mail.smtp.auth", "true");
    

    and it worked. all i did was add the port. and i also made my gmail accout access untrusted apps.