This discussion is archived
3 Replies Latest reply: Nov 11, 2012 1:52 PM by jschellSomeoneStoleMyAlias RSS

Java SMTP Client and Outlook unique behaviour

805947 Newbie
Currently Being Moderated
Hi

I have written a simple Java client to send a mail with an attachment to a mail address. Its a normal Java SMTP client with multi part file attachment.

There is a unique behavior happening which is eating my head for some time now. The issue here is that when i try to send an attachment from my Outlook the message is being delivered to the recipient successfully in recipients inbox. But when i send the same attachment through my standalone Java client its not getting delivered to the recipient(not 100 % sure, spamming maybe).The client is not throwing any exception also so its become very hard as to whats going on as i think the mail is getting delivered but not sure. The recipient email address is on a Linux box so don't know how to access it. please help me on this.

Thanks
Vikeng21
  • 1. Re: Java SMTP Client and Outlook unique behaviour
    EJP Guru
    Currently Being Moderated
    If your question is related to your Java code you would have to post it.

    If it isn't it is off topic.
  • 2. Re: Java SMTP Client and Outlook unique behaviour
    805947 Newbie
    Currently Being Moderated
    Hi EJP , Below is the code that i have written for the same


    import java.io.File;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.Part;
    import javax.mail.SendFailedException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    public class msgshow {
    public static void main(String[] args) throws Exception {
         
         String host = "***hostname***";
         String from = "***fromEmailId***";
         String to = "***toEmailId****";
         String filename = "log.txt";
         try{
              // Get system properties
              Properties props = System.getProperties();
         
              // Setup mail server
              props.setProperty("mail.smtp.host", host);
              //Doing this for testing as the properties are being set by using default system user properties.If user authentication is required
              boolean authValue = true;
              if(authValue){
                   props.setProperty("mail.user", "***username***");
                   props.setProperty("mail.password", "***password***");
              }
              props.setProperty("mail.debug", "true");
              // Get session
              javax.mail.Session session = javax.mail.Session.getInstance(props, null);
         
              // Define message
              Message message = new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.addRecipient(Message.RecipientType.TO,
              new InternetAddress(to));
              
              message.setSubject("VVVVVVVVSSSSSSSSSS");
         
              // Create the message part
              BodyPart messageBodyPart = new MimeBodyPart();
         
              // Fill the message
              messageBodyPart.setText("Here's the file");

              // Create a Multipart
              Multipart multipart = new MimeMultipart();
         
              // Add part one
              multipart.addBodyPart(messageBodyPart);
         
              //
              // Part two is attachment
              //
         
              // Create second body part
              messageBodyPart = new MimeBodyPart();
         
              // Get the attachment
              DataSource source = new FileDataSource(filename);
         
              // Set the data handler to the attachment
              messageBodyPart.setDataHandler(new DataHandler(source));
         
              //before setting the file name we have to set the proper file name
              final File individualFile = new File(filename);
              String fname = individualFile.getName();
              
              // Set the filename
              messageBodyPart.setFileName(fname);
              messageBodyPart.setDisposition(Part.ATTACHMENT);
         
              // Add part two
              multipart.addBodyPart(messageBodyPart);
         
              // Put parts in message
              message.setContent(multipart);
         
              // Send the message
              Transport.send(message);
              System.out.println("Message is sent");
         }catch(SendFailedException e){
              System.out.println("------------ Send Failed Exception -----------");
              Address adr[] = e.getValidSentAddresses();
              for(int i = 0; i < adr.length;i++){
                   System.out.println("------- Valid Sent address" + adr);
                   
              }
              Address adr1[] = e.getValidUnsentAddresses();
              for(int j = 0; j < adr1.length;j++){
                   System.out.println("------- Valid Sent address" + adr[j]);
                   
              }
              e.printStackTrace();          
         }catch(Exception ez){
              ez.printStackTrace();
         }

    }
    }
  • 3. Re: Java SMTP Client and Outlook unique behaviour
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    please help me on this.
    What I do is write (or find) a email server that lets me see the raw form of the email.
    Then send from both clients and compare the differences.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points