2016-12-06 1 views
0

この質問は他の人には返答されていましたが、私のためにはうまくいきませんでした。javax.mail.internet.ParseException:コンテンツタイプの文字列に<text>、 '/'、nullを返しました

基本的に私のコードはあなたの電子メールにログインし、複数の人にメッセージを送信します。

もちろん、私はそれを知っていますが、私は同じことをする私のコマンドラインプログラムの1つに基づいているため、十分なコードを持っています。

とにかくここにいくつかのコードがあります。

送信者クラス:

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

}

とはい私はこれを行うためのより効率的な方法があります知っているが怠惰な複数の方法が、イムを通じて必要なフィールドを送信するinsted。それで私がここに来た理由:p

あなたの助けに感謝し、他の3つのクラスが必要ならlmk。このエラーにはエラーが含まれているはずです。

答えて

0

私は基本的に私はこれにprops.put値を変更し、それを考え出しました。私がしたのはポートを追加することだけでした。私のGmailのアカウントを信頼できないアプリにもアクセスさせました。

0

message.setContent(content, "html")message.setText(content, null, "html")に置き換えます。 message.setContent(content, "text")message.setText(content)に置き換えてください。

+0

ummm setText()は複数の値ではない文字列しか持てないので、私にはエラーが表示されます – Fallspell

+0

申し訳ありませんが、MessageだけでなくMimeMessageとして宣言する必要があります。 –

+0

'Message message = new MimeMessage(session);' MimeMessageとして宣言しています.... – Fallspell

0

ビルの答えはそれをカバーしているようですが、まだあなたは固まっているようです。 1つの問題は、test/plainが必要なときにmessage.setContent(content, "text");がコンテンツタイプをtext/textに設定することです。次に例を示します。

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

、それが働いた:

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("===="); 
    } 
} 
関連する問題