2 Replies Latest reply: Oct 24, 2012 7:08 AM by User_Sep RSS

    mail with attachment

    User_Sep
      while running this code in any tool like eclipse,netbeans,jdeveloper means i got a mail with an attachment
      if am running this file in "D:\Oracle\Middleware\jdk160_24\bin" this section means.
      no error but no mail no attachment. what it would be a reason.
      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()) ;
                     }
                 }
              
          }