javax.mail.internet.ParseException:在Content-Type字符串< text>中,预期为'/',为null [英] javax.mail.internet.ParseException: In Content-Type string <text>, expected '/', got null

查看:213
本文介绍了javax.mail.internet.ParseException:在Content-Type字符串< text>中,预期为'/',为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.

发件人类别:

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

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

感谢您的帮助,如果您需要其他3个课程,则感谢lmk.但是错误应该出在这一点.

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

推荐答案

我知道了,基本上我将props.put值更改为此:

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

成功了.我所做的就是添加端口.并且我还使我的gmail帐户访问不受信任的应用程序.

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

这篇关于javax.mail.internet.ParseException:在Content-Type字符串&lt; text&gt;中,预期为'/',为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆