Email mit Anhang - File not Found

Nisbo

Bekanntes Mitglied
EDIT: Hat sich erledigt, habe eine andere Lösung, der Form halber würde es mich trotzdem interessieren was falsch war ?

Servus,

ich spiele gerade mit dem Emailversand etwas rum, Mails verschicken ohne Anhang geht, mi da bekomme ich

javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: ‪H:\Screenshot_6.jpg (Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1290)

Die Datei H:\Screenshot_6.jpg gibt es natürlich, habe da auch schon andere getestet.

Java:
String host = "smtp.gmail.com";
        String from = "xxx@gmail.com";
        final String user = "xxx@gmail.com";
        final String pass = "xxx";
     
        String ziel = "xxxxxxx";
        String filename = "‪H:/Screenshot_6.jpg";
     
        // SMTP Host setzen
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "" + 587);
        props.setProperty("mail.smtp.starttls.enable", "true");
     
        // Default Session
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            public javax.mail.PasswordAuthentication getPasswordAuthentication(){
                return new javax.mail.PasswordAuthentication(user, pass);
            }});
      
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ziel));

        // Set Subject: header field
        message.setSubject("Testing Subject");

        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();

        // Now set the actual message
        messageBodyPart.setText("This is message body");

        // Create a multipar message
        Multipart multipart = new MimeMultipart();

        // Set text message part
        multipart.addBodyPart(messageBodyPart);

        // Part two is attachment
        messageBodyPart   = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);

        // Send the complete message parts
        message.setContent(multipart);

        // Send message
        Transport.send(message);

        System.out.println("Sent message successfully....");

Also was ist falsch ?
 
Zuletzt bearbeitet:
OK ich habe es jetzt anders gelöst und gleich auch auf mehrere Dateianhänge erweitert, für die Suchfunktion hier der Code der funktioniert.


Java:
public static  void sendEmail() throws MessagingException {
        String host   = "smtp.gmail.com";
        String from   = "****@gmail.com";
        String user   = "****@gmail.com";
        String pass   = "****";
        String mailTo = "****";

        // attachments
        String[] attachFiles = new String[3];
        attachFiles[0] = "H:/Screenshot_6.jpg";
        attachFiles[1] = "H:/Screenshot_7.jpg";
        attachFiles[2] = "H:/Screenshot_8.jpg";
       
        // SMTP Host
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "" + 587);
        props.setProperty("mail.smtp.starttls.enable", "true");
      
        // Default Session
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            public javax.mail.PasswordAuthentication getPasswordAuthentication(){
                return new javax.mail.PasswordAuthentication(user, pass);
            }});
        // creates a new e-mail message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] toAddresses = { new InternetAddress(mailTo) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject("Subject");
        msg.setSentDate(null);
        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent("Nachtichtentext", "text/html");
        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        // adds attachments
        if (attachFiles != null && attachFiles.length > 0) {
            for (String filePath : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();
                try {
                    attachPart.attachFile(filePath);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                multipart.addBodyPart(attachPart);
            }
        }
        // sets the multi-part as e-mail's content
        msg.setContent(multipart);
        // sends the e-mail
        Transport.send(msg);
       
        System.out.println("Sent message successfully....");
    }
 

Zurück
Oben