javax.mail.internet.ParseException: In Content-Typ

2019-09-11 09:39发布

问题:

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.

回答1:

Replace message.setContent(content, "html") with message.setText(content, null, "html"). Replace message.setContent(content, "text") with message.setText(content).



回答2:

Bill's answer seem to cover it however it appears you are still stuck. One issue is that message.setContent(content, "text"); is going to set the content type to text/text when you want test/plain. Here is an example:

public class GMTCZ {
    public static void main(String[] args) throws Exception, Exception {
        MimeMessage mime = new MimeMessage((Session) null);

        mime.setText("That was 1 arg setText");
        print(mime);

        String charset = MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset());
        mime.setText("<html><title>This is 3 arg setText</title></html>", charset, "html");
        print(mime);

        mime.setText("That was 3 arg setText.", charset, "plain");
        print(mime);

        //OR

        Message msg = new MimeMessage((Session) null);
        msg.setContent("<html><title>That was 2 arg setContent</title></html>", "text/html");
        print(msg);

        msg.setContent("That was 2 arg setContent.", "text/plain");
        print(msg);

        msg.setContent("<html><title>That was 2 arg setContent.</title></html>", "text/html");
        print(msg);
    }

    private static void print(Message msg) throws Exception {
        msg.saveChanges();
        msg.writeTo(System.out);
        System.out.println();
        System.out.println("====");
    }
}


回答3:

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.