# Mail mit zip-Attachment



## Scoobie (1. Okt 2005)

Hallo Leute!

Ich habe folgendes Problem:
Ich möchte gerne eine Mail mit einem gezippten Attachment versenden, über mail.gmx.net

Ich bekomme aber immer die Fehlermeldung:

IOException while sending message;
nested Exception is:  javax.activation.UnsupportedDataTypeException: no Object DCH for MIME type multipart/mixed

die Sende-Routine sieht so aus:


```
public static void sendWithAuthenticatorAndAttachement (String userName, String password, String to, String from, String host, String betreff, String mailText, File attachFolder) throws Exception {

      // Get system properties
      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.auth", "true");

      // Setup authentication, get session
      Authenticator auth = new MyAuthenticator(userName, password);
      Session session = Session.getDefaultInstance(props, auth);

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(betreff);

      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(mailText);

      //Create Multipart
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);
      //Attachment
      messageBodyPart = new MimeBodyPart();
  
      /*/
      MIME Types
      txt - text/plain
      html - text/html
      xml - text/xml
      pdf - application/pdf
      zip - application/x-zip-compressed
      jpeg - image/jpeg
      gif - image/gif
      //*/

      messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(data),"application/x-zip-compressed"));
      //data ist eine temporäre Datei die gezippt vorhanden ist. Ist als Datentyp java.io.File im Speicher.

      multipart.addBodyPart(messageBodyPart);
      message.setContent(multipart);

      // Send message
      Transport transport = session.getTransport("smtp");
      transport.connect(host, userName, password);

      transport.sendMessage(message, message.getAllRecipients());
      transport.close();
    }
```

Bitte um Hilfe[/code]


----------



## Guest (11. Sep 2006)

Hi, ich habe das gleiche Problem, hast Du inzwischen eine Lösunng gefunden??


----------



## Autor (11. Sep 2006)

Dieser Codeschnipsel ist aus meinem Program herauskopiert worden. Das heißt einige Funktionen sind nicht dabei.
zB.: Class Attachment

Falls du gravierende Probleme damit hast, kann ich dir ja noch mehr senden, aber dieser grobe Umriss sollte dein Problem lösen.

Diese Klasse brauchst du auch:

```
class MyAuthenticator extends Authenticator {
  String username;
  String password;

  public MyAuthenticator(String username, String password){
    this.username = username;
    this.password = password;
  }

  public PasswordAuthentication getPasswordAuthentication() {
    /*/
    StringTokenizer st = new StringTokenizer(username + "," + password, ","); //keine Ahnung was ich mir dabei damals gedacht habe?????
    username = st.nextToken();
    password = st.nextToken();
    //*/
    return new PasswordAuthentication(username, password);
  }
}
```

Das ist aus meinem Hauptprogramm herauskopiert:


```
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;

  public static void sendWithAuthenticatorAndAttachement (String userName, String password, String to, String from, String host, String betreff, String mailText, File attachFolder,javax.swing.JButton send, javax.swing.JProgressBar bar) throws Exception {
      send.setVisible(false);

      // add handlers for main MIME types
      MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
      mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
      mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
      mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
      mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
      mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
      CommandMap.setDefaultCommandMap(mc);


      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.auth", "true");

      /*/POP IT
      String popHost = "pop.gmx.net";
      props.put("mail.pop3.host", popHost);
      //POP IT END*/

      // Setup authentication, get session
      Authenticator auth = new MyAuthenticator(userName, password);
      Session session = Session.getDefaultInstance(props, auth);

      /*/POP IT
      Store store = session.getStore("pop3");
      store.connect();
      Folder f = store.getFolder("INBOX");
      f.open(Folder.READ_ONLY);
      Message[] messages = f.getMessages();
      System.out.println(messages[0].getFrom()[0]);
      //POP IT END*/

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(betreff);

      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(mailText);
      if(debug) System.out.println("set MailText");

      //Create Multipart
      Multipart multipart = new MimeMultipart();
      if(debug) System.out.println("create MimeMultiPart");
      multipart.addBodyPart(messageBodyPart);
      if(debug) System.out.println("addBodyPart");
      //Attachment
      messageBodyPart = new MimeBodyPart();
      if(debug) System.out.println("create MimeBodyPart");
      /*/
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      //*/
      /*/
      MIME Types
      txt - text/plain
      html - text/html
      xml - text/xml
      pdf - application/pdf
      zip - application/x-zip-compressed
      jpeg - image/jpeg
      gif - image/gif
      //*/
      bar.setVisible(true);
      FileDataSource fds;
        fds = new FileDataSource(Attachment.zip(attachFolder, bar));

      bar.setVisible(false);
      send.setVisible(true);
      if(debug) System.out.println("load FileDataSource");
      //System.out.println(fds.getContentType());
      messageBodyPart.setDataHandler( new DataHandler(fds) );

      messageBodyPart.setFileName("data.zip");

      if(debug) System.out.println("set DataHandler");
      multipart.addBodyPart(messageBodyPart);
      if(debug) System.out.println("addBodyPart");
      message.setContent(multipart);
      if(debug) System.out.println("setContent MultiPart");

      // Send message
      Transport transport = session.getTransport("smtp");
      transport.connect(host, userName, password);
      if(debug) System.out.println("connect");

      transport.sendMessage(message, message.getAllRecipients());
      if(debug) System.out.println("send");
      transport.close();

      /*/POP IT
      f.close(false);
      store.close();
      //POP IT END*/
    }
```
[/code]


----------

