6 Replies Latest reply: Oct 24, 2012 7:02 AM by User_Sep RSS

    mailing

    User_Sep
      hi guys,
      i had question. i had a mail program in java with sending attachment.
      ok fine it's working good in tools like jdev,netbeans,ecliapse.

      but i want to run the same program in command prompt "D:\Oracle\Middleware\jdk160_24\bin"
      it's running. no error but no mail sending and no attachment sending.

      so my question is:? same program in jdev can able to send mail & with attachment.
      but in command prompt it doesnt work's so what would be a reason?

      anything special in jdev11.1.1.5.0?
        • 1. Re: mailing
          John Stegeman
          Who knows? You haven't shown us your code. You haven't shown us any log messages/errors/etc
          • 2. Re: mailing
            User_Sep
            no error.
            import java.io.*;
            import java.net.InetAddress;
            import java.util.Properties;
            import java.util.Date;
            import javax.mail.*;
            import javax.mail.internet.*;
            import javax.activation.*;
              
              public class NewTest
              {
                public static java.lang.String send() throws Exception
                {
                   String Attachment = "C:\\Users\\x.RMD\\Desktop\\x.png";
                   String rc ="";
                   int StartPos = 0;
                   int EndPos;
                   char LastAttachChar;
                   String Filename;
                   String operation = null;
                   int s=0;
                  //Check if attachment is null
                  if (Attachment == null||Attachment.equals(""))
                  {
                   EndPos=0;
                  }
                  else
                  {
                  EndPos=Attachment.length();
                  LastAttachChar = Attachment.charAt(EndPos-1);
                  //If Attachment is terminated by comma, delete the last character from attachment
                  if (LastAttachChar==',')
                  {
                  Attachment = Attachment.substring(StartPos,EndPos-1);
                  EndPos=Attachment.length();
                  }
                  }
                  //Get properties and default session for the SMTP server
                  Properties props = System.getProperties();                      
                  props.put("mail.smtp.host", "mail.mydomain.com");
                  props.put("mail.smtp.port", "587");
                  //props.put("mail.smtp.starttls.enable", "true");
                  props.put("mail.smtp.auth", "true");
                  Session session = Session.getDefaultInstance(props, null);
                  //Now try and create the mail message
                  try
                  {
                  //Create a message
                  Message msg = new MimeMessage(Session.getDefaultInstance(props, null));
                  //Add sender to message
                  operation = "FROM";
                  //msg.setFrom(new InternetAddress(From)); 
                  msg.setFrom(new InternetAddress("d@mydomain.com"));
                  //Add reciepient list to message
                  operation = "TO";
                  //InternetAddress[] address = InternetAddress.parse(To,false);
                  InternetAddress[] address = InternetAddress.parse("s@mydomain.com",false);
                  msg.setRecipients(Message.RecipientType.TO, address);
                  operation = "SUBJECT";
                  //Add subject to message
                  //msg.setSubject(Subject);
                  msg.setSubject("hi");
                  //Add date to message
                  msg.setSentDate(new Date());
                  operation = "NEW PART TO MESSAGE";
                  //Add a new part to the message
                  Multipart mp = new MimeMultipart();
                  {
                  MimeBodyPart mbp = new MimeBodyPart();
                  //Add the body to the new part
                  //mbp.setText(Body);
                  mbp.setText("hi");
                  mp.addBodyPart(mbp);
                  }
                  operation = "PARSE THE ATTACHMENT";
                  //Parse the attachment list and add files to the new part
                  if (EndPos != 0)
                  {
                  //Get the first and last positions of the file separator
                  int FilesepPos     = Attachment.indexOf(",");
                  int FilesepLastPos = Attachment.lastIndexOf(",");
                  //The attachment contains only one file
                  if (FilesepPos==-1)
                  {
                  FilesepPos=EndPos;
                  EndPos=0;
                  }
                  operation = "MIME";
                  while (true)
                  {
                  MimeBodyPart mbp = new MimeBodyPart();
                  Filename = Attachment.substring(StartPos,FilesepPos);
                  FileDataSource fds = new FileDataSource(Filename);
                  mbp.setDataHandler(new DataHandler(fds));
                  mbp.setFileName(fds.getName());
                  mp.addBodyPart(mbp);
                  operation = "ADD BODY PART";
                  if (EndPos==0)
                  {
                  break;
                  }
                  Attachment = Attachment.substring(FilesepPos+1,EndPos);
                  EndPos = Attachment.length();
                  if (FilesepPos==FilesepLastPos)
                  {
                  FilesepPos=EndPos;
                  EndPos=0;
                  }
                 else
                 {
                 FilesepPos    = Attachment.indexOf(",");
                 FilesepLastPos = Attachment.lastIndexOf(",");
                 }
                 }operation = "ATTACHMENT POSITION";
                 }
                 //Add the new part to the message
                 msg.setContent(mp);
                 operation = "SET CONTENT";
                //Send the message
                //Transport.send(msg);
                 operation = "SET TRANSPORT";
                 Transport tr = session.getTransport("smtp");
                 operation = "SET TRANSPORT 1";
                //tr.connect (SMTPhost, Username, Password);
                 tr.connect ("mail.mydomain.com", "d@mydomain.com", "dxxxxxxxx");
                 operation = "CONNECT";
                //msg.setContent(Body, "text/plain");
                  msg.saveChanges();
                  operation = "SAVE CHANGES";
                  tr.sendMessage(msg, msg.getAllRecipients());
                  operation = "SENT";
                  tr.close();
                  s=1;
                  rc = new String("Message Sent");
                //return new java.lang.String(rc);
                }
                catch (Exception aex)
                {
                rc = new String(aex.getMessage());
                //throw new Exception("Email Address error " + operation + " value > " + aex.getMessage());
                }
                finally
                {
                return new java.lang.String(rc);
                }
                }
                    public static void main(String args[]) 
                       {
                           NewTest e = new NewTest();
                           try {
                               e.send();
                           } catch (Exception f)
                           {
                              System.out.println("Excep" + f.getLocalizedMessage()) ;
                           }
                       }
                    
                }
            • 3. Re: mailing
              John Stegeman
              There's no error messages because you eat all exceptions and turn them into a return code, and you never print out the return code, so we have no idea what may have gone wrong.

              John
              • 4. Re: mailing
                User_Sep
                perfect answer from your previous post :)

                yes you are right absoultely. i got this exception.
                Exception in thread "main" javax.mail.MessagingException: IOException while send
                ing message;
                  nested exception is:
                        javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
                pe multipart/mixed; boundary="----=_Part_0_4115088.1351077337222"
                        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177)
                        at NewTest.send(NewTest.java:129)
                        at NewTest.main(NewTest.java:149)
                Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME
                 type multipart/mixed; boundary="----=_Part_0_4115088.1351077337222"
                        at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:87
                7)
                        at javax.activation.DataHandler.writeTo(DataHandler.java:302)
                        at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1485)
                        at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773)
                        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121)
                        ... 2 more
                but am quite understanble with jdev.
                it's perfectly sending mail with attachment in jdev

                Edited by: Arunkumar Kootharasan on Oct 24, 2012 4:48 PM
                • 5. Re: mailing
                  John Stegeman
                  When you Google for that exception "javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;" then all will become clear
                  • 6. Re: mailing
                    User_Sep
                    john is perfect always :) as my colleague told.

                    this one helped me while googling against this exception
                    Re: javax.activation.UnsupportedDataTypeException:

                    i found out my first mistake.
                    i extracted jar. as this thread says i did it.

                    second thing. i caught noclassdef error. i understood classpath is not setuped correctly.
                    so i follow this
                    http://www.codegravity.com/blog/exception-in-thread-main-java-lang-noclassdeffounderror
                    CLASSPATH=*.;*c:\...

                    after putting this dot and semicolon my program i identied the class path.

                    now the problem solved. now i got a mail with attachment.

                    atlast i conclude that, extracting jar file and classpath missing is my real problem for sending mail with attachment.

                    great john hat's off brovao thousands of applause for guiding me in right way :)