javax.mail.internet.ParseException:在 Content-Type 字符串 <text> 中,应为“/”,结果为空

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

我知道这个问题在其他地方得到了回答,但这对我不起作用,所以我来了。

基本上我的代码将登录到您的电子邮件并向多个人发送消息。

当然它还没有完成,我知道,但是它有足够的代码来工作,因为我基于我的一个命令行程序来做同样的事情。

无论如何,这是一些代码。

发件人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);
}   

}

是的,我知道有更有效的方法来执行此操作,而不是通过多种方法发送所需的字段,但我很懒。这就是我来这里的原因:p

感谢您的帮助,如果您想要其他 3 个 classes,请联系我。但是错误应该在这一个。

message.setContent(content, "html") 替换为 message.setText(content, null, "html")。将 message.setContent(content, "text") 替换为 message.setText(content).

Bill 的回答似乎涵盖了它,但您似乎仍然被困住了。一个问题是 message.setContent(content, "text"); 将在您需要 text/plain 时将内容类型设置为 text/text。这是一个例子:

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("====");
    }
}

我想通了,基本上我将 props.put 值更改为:

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");

它奏效了。我所做的就是添加端口。而且我还让我的 gmail 帐户访问不受信任的应用程序。