Problems with Javamail Attachments

Hi,
i've got a major problem with my application and i can't find the answer. Whenever i'm trying to send an attachment i have this error :
javax.mail.internet.ParseException
at javax.mail.internet.ParameterList.<init>(ParameterList.java:77)
at javax.mail.internet.ContentDisposition.<init>(ContentDisposition.java:69)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1104)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:824)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:215)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1056)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1914)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1895)
at javax.mail.Transport.send(Transport.java:79)
at Send$5.run(Send.java:209)
at java.lang.Thread.run(Thread.java:536)
Interactive Session Ended
My source code is :
public class DemoSend implements ActionListener{
public void actionPerformed(ActionEvent evt) {
try {
final Properties props = System.getProperties();
props.put("mail.host", hostField.getText());
final Session mailConnection = Session.getDefaultInstance(props, null);
final MimeMessage msg = new MimeMessage(mailConnection);
final Address to = new InternetAddress(toField.getText());
final Address from = new InternetAddress(fromField.getText());
msg.setContent(message.getText(), "text/plain");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(subjectField.getText());
msg.setSentDate(new java.util.Date());
final MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(message.getText());
final MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setDisposition(mbp2.ATTACHMENT);
final FileDataSource fds=new FileDataSource(FileName);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
final Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setSentDate(new Date());
msg.setContent(mp);
Runnable r = new Runnable() {
public void run() {
try {
Transport.send(msg);
JOptionPane.showMessageDialog(null," Your message has been sent successfuly","G T P Email Client",JOptionPane.INFORMATION_MESSAGE);
catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null," -- E r r o r -- Your message has not been sent!!!!! ","G T P Email Client",JOptionPane.INFORMATION_MESSAGE);
Thread t = new Thread(r);
t.start();
message.setText("");
catch (Exception e) {
e.printStackTrace();
and i define at the beggining the string FileName
public static String FileName = new String();
and thats for the attach button
attachButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (fc == null) {
fc = new JFileChooser();
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(true);
fc.setFileView(new ImageFileView());
fc.setAccessory(new ImagePreview(fc));
fc.setMultiSelectionEnabled(true);
int returnVal = fc.showDialog(Send.this,"Attach File");
fc.getSelectedFile();
File file = fc.getSelectedFile();
It would be a great help if you can can help me find an answer to my problem.
Thanks in advance.

I tried to set a specific file for the FileChooser but again the same results.I tried without the preview classes again the same mistake.But when i tried without the JFileChooser it worked, but i must use a JFileChooser in order to choose a file to attach.I used a simple FileChooser and again the same mistakes.I don't know where the mistake is.
I will be grateful if you can help me out,this is an emergency it is for my project and i'm desperate.
I include the whole class.
Thanks again and sorry for the long message.
I hope you have some free time to help me out.
Thanks again
import java.awt.*;
import javax.swing.JFileChooser.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.util.Properties;
public class Send extends JFrame {
     public static void main(String[] args) throws Exception{
          Send client = new Send();
          client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          client.show();
     public static JLabel fromLabel = new JLabel("From: ");
          public static JLabel toLabel = new JLabel("To: ");
          public static JLabel hostLabel = new JLabel("SMTP Server: ");
          public static JLabel subjectLabel = new JLabel("Subject: ");
          public static JTextField fromField = new JTextField(40);
          public static JTextField toField = new JTextField(40);
          public static JTextField hostField = new JTextField(40);
          public static JTextField subjectField = new JTextField(40);
          public static JTextArea message = new JTextArea(40, 80);
          public static JScrollPane jsp = new JScrollPane(message);
     public static JButton sendButton = new JButton("Send Message");
     public static JButton clearButton = new JButton("Clear");
     public static JButton attachButton = new JButton("Attach File");
     public static JButton adbButton = new JButton("AddressBook");
     public static JFileChooser fc;
     public static String FileName = new String();
     public Send() {
          super("SMTP Send email");
          Container contentPane = this.getContentPane();
          contentPane.setLayout(new BorderLayout());
          JPanel labels = new JPanel();
          labels.setLayout(new GridLayout(4, 1));
          labels.add(hostLabel);
          JPanel fields = new JPanel();
          fields.setLayout(new GridLayout(4, 1));
          final String host = System.getProperty("mail.host", hostField.getText());
          hostField.setText(host);
          fields.add(hostField);
          final String to = System.getProperty("mail.to", toField.getText());
          labels.add(toLabel);
          fields.add(toField);
          final String from = System.getProperty("mail.from", fromField.getText());
          fromField.setText(from);
          labels.add(fromLabel);
          fields.add(fromField);
          labels.add(subjectLabel);
          fields.add(subjectField);
          Box north = Box.createHorizontalBox();
          north.add(labels);
          north.add(fields);
          north.add(attachButton);
          north.add(adbButton);
          contentPane.add(north, BorderLayout.NORTH);
          message.setFont(new Font("Arial", Font.PLAIN, 12));
          contentPane.add(jsp, BorderLayout.CENTER);
          JPanel south = new JPanel(new FlowLayout());
          south.setLayout(new FlowLayout(FlowLayout.CENTER));
          south.add(sendButton);
          south.add(clearButton);
          contentPane.add(south, BorderLayout.SOUTH);
          this.pack();
          adbButton.addActionListener(new ActionListener()
                    public void actionPerformed(ActionEvent e)
                         AddressBook adbook = new AddressBook();                     
          clearButton.addActionListener(new ActionListener()
                    public void actionPerformed(ActionEvent e)
                         fromField.setText("");
                         toField.setText("");      
                         hostField.setText("");     
                         subjectField.setText("");
                         message.setText("");
          attachButton.addActionListener(new ActionListener()
                         public void actionPerformed(ActionEvent e)
if (fc == null) {
                         fc = new JFileChooser();
                              fc.addChoosableFileFilter(new ImageFilter());
                              fc.setAcceptAllFileFilterUsed(true);
                              fc.setFileView(new ImageFileView());
                              fc.setAccessory(new ImagePreview(fc));
                              fc.setMultiSelectionEnabled(true);
                         int returnVal = fc.showDialog(Send.this,"Attach File");
                         File file= fc.getSelectedFile();
                         //fc.getSelectedFile();
                         /* JFileChooser fc = new JFileChooser();
                         int retval = fc.showOpenDialog(Send.this);
                    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    fc.setMultiSelectionEnabled(true);
                    File file = fc.getSelectedFile();*/
          DemoSend handler = new DemoSend();
          sendButton.addActionListener(handler);
          addWindowListener(new WindowAdapter()
                    public void windowClosing (WindowEvent e)
          setSize (600, 500);
          show();
     public class DemoSend implements ActionListener{
          public void actionPerformed(ActionEvent evt) {
               try {
                    final Properties props = new Properties();
                    props.put("mail.host", hostField.getText());
                    final Session mailConnection = Session.getDefaultInstance(props, null);
                    final MimeMessage msg = new MimeMessage(mailConnection);
                    final Address to = new InternetAddress(toField.getText());
                    final Address from = new InternetAddress(fromField.getText());
                    //msg.setContent(message.getText(), "Content-Transfer-Encoding ,base64");
                    msg.setFrom(from);
                    msg.setRecipient(Message.RecipientType.TO, to);
                    msg.setSubject(subjectField.getText());
                    msg.setSentDate(new java.util.Date());
                    final MimeBodyPart mbp1 = new MimeBodyPart();
                    final MimeBodyPart mbp2 = new MimeBodyPart();
                    mbp1.setContent("Content-Transfer-Encoding", "base64");
                    mbp1.setText(message.getText());
                    //if (!FileName.equals("")) {     
                    mbp2.setDisposition(mbp2.ATTACHMENT);
                    mbp2.setContent("Content-Transfer-Encoding", "base64");
                    final FileDataSource fds=new FileDataSource(FileName);
                    mbp2.setDataHandler(new DataHandler(fds));
                    mbp2.setFileName(fds.getName());
                    final Multipart mp = new MimeMultipart();
                    mp.addBodyPart(mbp1);
                    //if (!FileName.equals("")) {
                    mp.addBodyPart(mbp2);
                    msg.setSentDate(new Date());
                    msg.setHeader("Content-Type", "multipart/mixed");
                    msg.setContent(mp);
                    Runnable r = new Runnable() {
                         public void run() {
                              try {
                                   Transport.send(msg);
                                   JOptionPane.showMessageDialog(null," Your message has been sent successfuly","G T P Email Client",JOptionPane.INFORMATION_MESSAGE);
                              catch (Exception e) {
                                   e.printStackTrace();
                                   JOptionPane.showMessageDialog(null," -- E r r o r -- Your message has not been sent!!!!! ","G T P Email Client",JOptionPane.INFORMATION_MESSAGE);
                    Thread t = new Thread(r);
                    t.start();
               catch (Exception e) {
                    e.printStackTrace();
}

Similar Messages

  • Problem with Javamail in Red Hat

    Good afternoon.
    I have a problem with javamail in Red Hat.
    When i send a email in windows with my application, works well. But in Red Hat no works.
    Gives the following error:
    Java.lang.ClassCastException .... gnu.mail.handler.TextPlain
    Does anyone know that happens?

    You have much to learn. This is the wrong forum for most of it.
    You should be able to use the package manager in Linux to
    uninstall the Gnu version. Then read this:
    [http://java.sun.com/products/javamail/FAQ.html#install|http://java.sun.com/products/javamail/FAQ.html#install]

  • Problem with PDF attachments on Leopard's mail.

    I just found a bug or something in Leoprd's mail . When I try to send a mail with PDF attachments, mail just stuck, nothing happen. The fan start to spin and mail grab all of the processors power nearly 100 %. What's weird, there aren't problem with sending other attachment like JPG for example. I tested it on all of my accounts and on all of them, situation is exactly the same.

    The same problem here! Mail.app is behaving weirdly, but eventually it stopped sending e-mail with some filetypes attachments (.doc, .xls, .pdf not working, .jpg working), app just freezes - impossible to quit! HEEEELP!!!

  • Problems with opening attachments sent from Microsoft Outlook

    I have been using MacBook Pro and I have had problems with opening files from Microsoft Outlook.
    The attachments appear as .dat files (text, images etc..) and I am not able to open them.
    Does someone have any experience in solving this type of compatability problems on Mac OS ?

    Get this app or tell the sender to send the attachments using plain text for their emial:
    http://www.joshjacob.com/mac-development/tnef.php

  • Problem with mail attachments

    I have an xlsx file I have opened on my iPad and iPhone before, but it won't open on either now. If I go through Safari mail it works fine, how can I open it from my mail again? When I press on the downward arrow nothing happens. Thanks in advance

    You have mentioned both xlsx and PDF - confusing!
    Anyhow, can you go into your emails via Safari, forward the email in question to yourself again and before trying to open it, delete the existing one on your iPad which you have problems with. Then try the newly forwarded one.

  • Problems with opening attachments

    Received a 14 page attachment to an email. Playbook  Adobe  reader only showed first page although it indicated there were 14 pages. ( No problem with  reading document on my computer.)
    Logging on to my Bank via internet I have been unable to open messages. This was not an issue last week. Now I get a message " file exists Rename this file or press save to replace existing file" Then I get a blank screen. All I am trying to do is open and read the message in my browser..

    Partially solved the Adobe reader issue. If I simultaneously swipe down from the top and up from the bottom I get to a bar which advances the pages. I have to do this for each page as the bar keeps disappearing. Must be an easier method. 

  • Problem with sending attachments over 1mb

    anyone faced that
    There may be a problem with the mail server or network. Check the settings for account “Gmail” or try again.
    The server error encountered was: The connection to the server “pop.gmail.com” on port 995 timed out.
    Checked already many options i.e. 465, ssl, no ssl etc.. seems nothing works
    No problem on PC with the same account

    lizak wrote:
    anyone faced that
    There may be a problem with the mail server or network. Check the settings for account “Gmail” or try again.
    The server error encountered was: The connection to the server “pop.gmail.com” on port 995 timed out.
    Checked already many options i.e. 465, ssl, no ssl etc.. seems nothing works
    No problem on PC with the same account
    welcome to discussions, lizak !
    have you tried zipping the file(s) ?

  • Problems with pdf attachments in Mail.app

    Any pdf attachment in Mail.app says it's empty and when I double click it says it can't be opened ´cause it's empty. The thing is on Windows using Thunderbird (yes I know, I've spoken the name that can't be spoken) the same attached pdf shows normally.
    I have no antivirus in the mac, or any type of filtering. In fact, Mavericks was clean installed about a month ago. Attachments arrive from Gmail. From the Gmail web interface, the file is fine and opens normally.
    Any ideas?

    Hi Krischi,
    sounds good (for you).
    What do you mean exactly by "deleted iCloud"?
    I do experience the problem not only with my iCloud mail account, but with all others also.
    Are you sure you have 7.0"3"? For me it says 7.02 is the newest and current version.
    Brgds,
    Peter
    Hi Peter,
    Writing my post I made an mistake! I deleted my app CloudOn instead of iCloud and as soon I did it, I could handle my PDF attachment as before using iOS5.x!
    The reason for this might be the fact, that CloudOn currently, according to the AppStore, is not upgraded to iOS 7.0.2!
    Regarding iOS 7.0.3 it was a typo! I'm using 7.0.2
    Hope that helps
    Christian

  • Problem with iWorks attachments

    I recently purchased the latest version of iWorks so that I can send attachments to my iPhone 3G, but I have not been able to open any attachments saved in any of the iWorks formats (.numbers, .pages or .key). The files have the proper extensions and appear with the appropriate icon in mail on the iPhone, but will only open to a blank screen or with a very small question mark in the upper left hand corner. I have tried sending the attachments using Entourage, Apple Mail and my Mobile Me (web mail) account all with the same result.
    Is any one else having this problem? Any ideas on what is causing it?

    I can convert the files to other formats like Word, Excel, Power Point and PDF. This works, but it is my understanding that the native format of iWorks should be recognized in email on the iPhone. I would like to skip the conversion step.

  • Trivial problem with SOAP attachments

    Hi,
    I'm writing a very simple standalone JAXM client which will send a SOAP message with attachments to a listening servlet. I can get the code to work for a SOAP message without attachments with no problems, but when I try to create an AttachmentPart object, there is a problem when running the code (there is an exception when I try to create an AttachmentPart object) even though the code compiles ok.
    It must be something really obvious, and I've tried copying examples from the JWSDP tutorial but to no avail! Any ideas welcome!
    import javax.xml.soap.*;
    import java.util.*;
    import java.net.URL;
    public class JAXMClient{
    public static void main(String[] args){
         String me = "Simon";
         try{
         SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
         SOAPConnection con = scFactory.createConnection();
         MessageFactory factory = MessageFactory.newInstance();
         SOAPMessage message = factory.createMessage();
         // create attachment
         AttachmentPart attachment = message.createAttachmentPart();
         attachment.setContent(me, "text/plain");
         attachment.setContentId("my_name");
         message.addAttachmentPart(attachment);
         URL endpoint = new URL("http://localhost:8080/EGSO/TestServlet");
         System.out.println("Calling service....");
         SOAPMessage response = con.call(message,endpoint);
         System.out.println("Reply received!");
         response.writeTo(System.out);
         con.close();
         catch(Exception e){
         System.out.println("Error somewehere");

    I use the below code to add attachments (from JWSDP tutorial)
    StringBuffer urlSB = new StringBuffer();
    urlSB.append(req.getScheme()).append("://").append(req.getServerName());
    urlSB.append( ":" ).append( req.getServerPort() ).append( req.getContextPath() );
    String reqBase = urlSB.toString();
    AttachmentPart apText = msg.createAttachmentPart(new DataHandler(new URL(reqBase + "/test.html")));
                   apText.setContentType("text/html");
    msg.addAttachmentPart(apText);
    (where msg is the SOAPMessage, and the "test.html" file resides in your servlet container's ROOT dir (eg $TOMCAT_HOME/webapps/ROOT)
    Ben

  • Problem with Mail-Attachments in an Unicode-Systems

    Hi everybody,
    we have a problem mailing a attachment by using the class CL_DOCUMENT_BCS in a Unicode system, WAS 7.0
    We attach a textfile using the method
        CALL METHOD lr_document->add_attachment
            EXPORTING
              i_attachment_type    = lv_extension
              i_attachment_subject = lv_dateiname
              i_att_content_text   = oti_object.
    The mail should have  a TXT-file in ASCII.
    With NOTEPAD, the attachment looks fine.
    However with WORDPAD and other applications, all german characters are two bytes, so it seems to be in a UTF-8 format.
    Is it possible to say the class, that the attachment should be ASCII?
    Regards,
    Stefan

    Hi,
    Try to use
      try.
          a_sofm_document = cl_document_bcs=>create_from_text(
                       i_text = text
                    i_subject = subject ).
        catch cx_bcs into bcs_exception.
          error_handling
          'cl_document_bcs=>create_from_text' bcs_exception->error_type.
      endtry.
    ** attachment
      if w_att = 'X'.
         try.
            call method a_sofm_document->add_attachment(
                      i_attachment_type = 'RAW'
                      i_attachment_subject = 'My attachment'
                      i_attachment_size = '21'
                      i_att_content_text = text ).
          catch cx_bcs into bcs_exception.
            error_handling 'a_sofm_document->add_attachment'
                                          bcs_exception->error_type.
        endtry.
      endif.

  • Problems with PDF attachments in Mail in ios7

    When I get a PDF in an email in ios7 it will not let me open it with another app. It will only save it as an image and if there is more than one page it only displays the first page. How can I get the file to open in another app so I can view it.

    Hi Krischi,
    sounds good (for you).
    What do you mean exactly by "deleted iCloud"?
    I do experience the problem not only with my iCloud mail account, but with all others also.
    Are you sure you have 7.0"3"? For me it says 7.02 is the newest and current version.
    Brgds,
    Peter
    Hi Peter,
    Writing my post I made an mistake! I deleted my app CloudOn instead of iCloud and as soon I did it, I could handle my PDF attachment as before using iOS5.x!
    The reason for this might be the fact, that CloudOn currently, according to the AppStore, is not upgraded to iOS 7.0.2!
    Regarding iOS 7.0.3 it was a typo! I'm using 7.0.2
    Hope that helps
    Christian

  • Problems with receiving attachments in Mail

    Hi. Can somebody help me. I work in Mail but keep on having problems receiving attachments from my clients (most of whom use Microsoft). I seem to receive their attachments in a 'dat' format - which does not open. Is can this be fixed? As always thank you all!

    Josh Jacob - TNEF's Enough

  • Problem with JavaMail 1.1 Api

    I have written a stateles session bean that should send a email using
              JavaMail 1.1 API. When running as it is now I receive a MessagingException:
              "No provider for Address type: rfc822". How do I grant permission to the
              mail.jar and activiation.jar files so that WL get it ? Im using Windows NT.
              Any help is greatly appreciated.
              Regards,
              Johan.
              

    SP2 is out now also - so please try it and let me know. I am trying to use the
              JavaMail stuff also. I do not seem to be having a problem on SP2 so far.
              - Tom
              Michael Girdley wrote:
              > I understand that this is a bug that is to be fixed in service pack No. 2.
              >
              > Thanks,
              > Michael
              >
              > --
              > ----
              > Michael Girdley
              > Product Manager, WebLogic Server & Express
              > BEA Systems Inc
              >
              > johan sanden <[email protected]> wrote in message
              > news:8hipsh$dur$[email protected]..
              > > I have written a stateles session bean that should send a email using
              > > JavaMail 1.1 API. When running as it is now I receive a
              > MessagingException:
              > > "No provider for Address type: rfc822". How do I grant permission to the
              > > mail.jar and activiation.jar files so that WL get it ? Im using Windows
              > NT.
              > >
              > > Any help is greatly appreciated.
              > >
              > > Regards,
              > >
              > > Johan.
              > >
              > >
              

  • Problems with document attachments

    I generally look at email as web mail. When senders send me Word documents they often don't show up as separate attachments. A strange detail is that the total size of the message, e.g., 120 KB, shows that the document is there somewhere, but is not visible.
    By contrast, .rtf files always work: they show up clearly.
    I would appreciate any thoughts, thank you in advance.

    Hi Karri,
    Believe what you are looking  at is a direct translation of Open XML Documents (.DOCX) to HTML, enabling access to the information in the Open XML format from any platform with a Web browser i.e, Document viewer in this case.A plug-in for Firefox, IE7 and IE8 is available  that allows users to view Open XML documents (.DOCX) within the browser on Windows and Linux platforms.
    Trust the below link will aid you resolve this:
    http://blogs.msdn.com/interoperability/archive/2009/05/17/openxml-document-viewer-v1-released-viewing-docx-files-as-html.aspx
    Regards,
    Pradeepkumar Haragoldavar

Maybe you are looking for

  • Adobe Acrobat XI Pro - cannot sign-in

    I've downloaded Adobe Acrobat XI Pro and cannot sign in. Get an error message that cannot access Internet.  Not the case, I'm connected.  Checked FAQ, all certificates are there, enabled, not problem with firewall.  What next?

  • Data storage in LV 7.1

    Is anyone familiar with the new storage tool in LV 7.1? I have been able to write to an excel file, but only the file specs are written, not my input?!?!? Does anyone know what I am missing? I have attached my small program (the file path should be c

  • Certificat question

    Hello Friends. PERFORM subroutine USING var. The var field is known as what type of parameter? A: Formal B: Actual C: Static D: Value thanks in advance, Moderator message - Please see Please read "The Forum Rules of Engagement" before posting!  HOT N

  • I tried updating my iphone with syncing and now it says it is in recovery mode, waht do i do?

    my iphone 4s locked out when i tried to update the OS.  now the macbook says iphone is in recovery mode.  what do i do?

  • History not restoring to safari

    After doing a safari history pllst restore from time machine the report comes up in preview screen but is not coming up when i go back into safari history. is it going somewhere else?  thanks