Hey I have this following code:

try
        {
            // Connection props
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", host);
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "25");
            props.setProperty("mail.smtp.user", usuario);
            props.setProperty("mail.smtp.auth", "true");
            
            // Prepare the session
            Session session = Session.getDefaultInstance(props);

            // Constuct the message
            MimeMessage message = new MimeMessage(session);
            /*Test*/   message.setHeader("Content-Type", "multipart/mixed");
            message.setFrom(new InternetAddress(CorreoOrigen));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(CorreoDestino));
            
            message.setSubject(asunto);
       
            
            Multipart mp = new MimeMultipart(); 
            BodyPart texto=new MimeBodyPart();
            /*Test*/   texto.setHeader("Content-Type", "multipart/mixed");
            texto.setContent(mensaje,"text/html");
            mp.addBodyPart(texto);
            MimeBodyPart ajunto=new MimeBodyPart();
            ajunto.setHeader("Content-Type", "multipart/mixed");
            ajunto.attachFile(NombreArchivo);
            mp.addBodyPart(ajunto);
            
            message.setContent(mp);
            
         //   message.setContent(mp, "text/html");
            
            // We send it
            Transport t = session.getTransport("smtp");
            t.connect(usuario,contraseña);
            t.sendMessage(message, message.getAllRecipients());

            // Close.
            t.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

Everytime I send it, it gets sent to the junk folder in Outlook 2007 and I rather it good directly to inbox. It contains simple text (nothing more, I put HTML in case we want to make it "prettier" in the future) and a attachment which can be a generated PDF or a generated ZIP. Once moved to the inbox, the PDF and the ZIP and be downloaded perfectly and opened and its content can be viewed so there is nothing stange or anything about it.

How can I fix this so it goes directly to the inbox? Thank you.

BTW, /*Test*/ lines did not work.

Dani AI

Generated

The symptoms — PDF sometimes goes to Inbox while ZIPs consistently land in Junk — point less to the Java code alone and more to a mix of attachment type, message headers and sender reputation. is right that recipient-side whitelisting is a quick fix, and is right that where mail comes from (domain/IP reputation) matters. Two important, practical fixes in the code: do not force Content-Type headers on the message or individual parts (the /Test/ lines); JavaMail will build correct MIME boundaries. Also include a plain-text alternative when sending HTML, because lacking it raises spam scores.

Checklist to apply (server + message hygiene)

  • Publish SPF, sign with DKIM and publish a DMARC policy so recipient gateways can authenticate the mail.
  • Ensure the sending IP has a proper PTR (reverse DNS) and isn’t a consumer/dynamic IP or on RBLs.
  • Keep envelope-from/Return-Path aligned with the From domain and HELO/EHLO.
  • Let JavaMail set MIME headers; attach files with a proper DataSource so attachments carry real MIME types (application/pdf, application/zip) and correct Content-Disposition and encoding.
  • Avoid password‑protected or encrypted ZIPs and avoid putting executables inside archives — those raise immediate red flags.
  • Add a clear plain-text line in the body describing the attachment (filename, sender, purpose).

Why ZIPs get flagged and practical alternatives
ZIPs can hide malicious files and many gateways block encrypted archives because they can’t scan inside. Safer approaches: host the archive on a secure HTTPS download link (with short expiry), send individual safe-format files, or sign the message (DKIM or S/MIME) so receivers can verify authenticity.

Debug steps
Inspect the raw headers in Outlook (Message Options) for Authentication-Results, Received-SPF, DKIM-Signature and any anti-spam fields (X-Forefront-Antispam-Report, X-MS-Exchange-Organization-SCL). Send comparison tests to Gmail/Outlook and adjust based on those results. White-listing by recipients helps short-term; long-term deliverability requires the checklist above.

Recommended Answers

All 7 Replies

I don't think there's anything you can do in your sending program to bypass Outlook's junk filtering rules. (If there was, then then junk filtering would be useless.)
You have to configure Outlook not to junk those messages, eg by white-listing the sender's domain.

The PDF now goes to the inbox for some reason....

Member Avatar for Member #647493

Maybe it is YOU!
Junk mail filters usually work on WHERE e-mail is coming FROM (ie. your domain).

Maybe it is YOU!
Junk mail filters usually work on WHERE e-mail is coming FROM (ie. your domain).

The thing is then why does the PDF work and not the zip...

Yes, you can whitelist/blacklist by the sender's name or domain, but there are a load of other ad-hoc algorithms used by Outlook's junk filter eg presence of certain keywords, or attachment types (pdf is generally "safer" than zip).

Member Avatar for Member #647493

JamesCherrill is correct!
While *.pdf files are documents, *.zip files can contain an undesirable little "gem".

Damn......oh well. Ill include some details in the email but I dont like this "solution"....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.