Sending an e-mail using java

Hi,
I am sending an e-mail using java. It works fine, but I wanted to know if there is any possibility to attach a document, without using a File object. Because I have to write the File object and then delete it at the end. I would like to know if I can use for example a byte [], or something else.
Thank you very much.
This is my code, if that helps.
public static void sendWithAttach(String p_sender, String p_receiver, String p_subject,
String p_comment, int p_nombrePiecesJoints, HashMap<String, byte[]> p_files) {
Properties props = System.getProperties();
props.put("mail.smtp.host", Solapcore.getCurrent().getServletConfig().getServletContext().getInitParameter("SMTP"));
Session session = Session.getDefaultInstance(props, null);
MimeMessage sender = new MimeMessage(session);
MimeBodyPart mbp1 = new MimeBodyPart();
Multipart mp = new MimeMultipart();
List<File> listeFiles = new ArrayList<File>();
try {
//On assigne to, from, subject, body, priority
sender.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(p_receiver, false));
sender.setFrom(new InternetAddress(p_sender));
sender.setSubject(p_subject);
mbp1.setText(p_comment);
//sender.setPriority(0);
mp.addBodyPart(mbp1);
//On attache les documents
for(Map.Entry<String, byte[]> entry : p_files.entrySet()) {
MimeBodyPart mbp2 = new MimeBodyPart();
String fileName = entry.getKey();
byte [] dataFile = entry.getValue();
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(dataFile);
mbp2.attachFile(file);
mbp2.setFileName(fileName);
mp.addBodyPart(mbp2);
listeFiles.add(file);
sender.setContent(mp);
Transport.send(sender);
}catch(MessagingException mex) {
mex.printStackTrace();
}catch(FileNotFoundException fnf) {
fnf.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}finally {
Iterator<File> itListeFiles = listeFiles.iterator();
while(itListeFiles.hasNext()) {
File tempFile = (File)itListeFiles.next();
tempFile.delete();
}

Thank you very much.
   public static void sendWithAttach(String p_sender, String p_receiver, String p_subject,
      String p_comment, int p_nombrePiecesJoints, HashMap<String, byte[]> p_files) {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", Solapcore.getCurrent().getServletConfig().getServletContext().getInitParameter("SMTP"));
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage sender = new MimeMessage(session);
    MimeBodyPart mbp1 = new MimeBodyPart();
    Multipart mp = new MimeMultipart();
    List<File> listeFiles = new ArrayList<File>();
    try {
      //On assigne to, from, subject, body, priority
      sender.setRecipients(javax.mail.Message.RecipientType.TO,
          InternetAddress.parse(p_receiver, false));
      sender.setFrom(new InternetAddress(p_sender));
      sender.setSubject(p_subject);
      mbp1.setText(p_comment);
      //sender.setPriority(0);
      mp.addBodyPart(mbp1);
      //On attache les documents
      for(Map.Entry<String, byte[]> entry : p_files.entrySet()) {
        MimeBodyPart mbp2 = new MimeBodyPart();
        String fileName = entry.getKey();
        byte [] dataFile = entry.getValue();
        File file = new File(fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(dataFile);
        mbp2.attachFile(file);
        mbp2.setFileName(fileName);
        mp.addBodyPart(mbp2);
        listeFiles.add(file);
      sender.setContent(mp);
      Transport.send(sender);
    }catch(MessagingException mex) {
      mex.printStackTrace();
    }catch(FileNotFoundException fnf) {
      fnf.printStackTrace();
    }catch(IOException ioe) {
      ioe.printStackTrace();
    }finally {
      Iterator<File> itListeFiles = listeFiles.iterator();
      while(itListeFiles.hasNext()) {
        File tempFile = (File)itListeFiles.next();
        tempFile.delete();
    }

Similar Messages

  • How to send HTML Format Mail using Java Mail in oracle 9i Forms

    Dear All
    could you please tell me how to send HTML Format Mail using Java Mail in oracle 9i Forms and how to implement the java mail ?
    if it is possible, could you please send me the sample code? please very urgent
    Thanks
    P.Sivaraman

    Hello,
    <p>Here is a Form sample.</p>
    Francois

  • Sending HTML E Mail using Java Mail

    Can anybody please tell me how to send HTML email using JavaMail. I know I need the DataContentHandler for text/html mime type but where can I find one? If anybody has better suggestions you are welcome.

    Folks,
    I've used what you have suggested. Though it compiled, I kept getting this java error
    --Exception in MailServer.java
    javax.mail.SendFailedException: Sending failed;
    nested exception is:
    javax.mail.MessagingException: IOException while sending message;nested exception is:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/html
    at javax.mail.Transport.send0(Transport.java:204)
    at javax.mail.Transport.send(Transport.java:73)
    at MailTest.send(MailTest.java:419)
    at MailTest.sendThruDefaultServer(MailTest.java:319)
    at MailTest.main(MailTest.java:476)
    [sendEmail() - Exception  :javax.activation.UnsupportedDataTypeException
    HELP!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Mail using java

    Hello Every one i just want send a simple mail using java or jsp.. so can any one help me ?

    Please note that this forum is focused on the use and support of the Oracle Enterprise Pack for Eclipse. There are better, more appropriate forums for Java technologies such as Java Mail. You should do a search for the Java Mail API. You will find the FAQ for the Java Mail API at http://www.oracle.com/technetwork/java/faq-135477.html and there will be many results with examples for sending mail. One such article is http://www.java-tips.org/other-api-tips/javamail/sending-e-mail-using-javamail-api.html

  • How can i send and recieve mail through java

    HI All,
    plz let me know how can i send and receive mail through java.
    Regards:
    sachin kakkar
    http://www.datamansoftwares.com

    Using java mail API..
    try to search the forum for java mail... you find many question and answer.
    use the left search textfield

  • Can any one please send me sample programs using java mapping..

    <b>Can any one please send me sample programs using java mapping with input and output?</b>
    please let me know how to get started with java mapping?
    Prerequisites for java mapping ?
    What are the jars need to be deployed for SAX,DOM.  And how to do it in NWDS?
    Use of Execute() and setparameter() ... StreamTransformation?
    Some sample program using java mapping with simple logic(input and output)?

    Hi,
    Did you go thro these blogs?
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-iii
    Nice blogs to start Java Mapping.
    Go thro this one too.
    /people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio
    Regards,
    P.Venkat
    Message was edited by:
            Venkataramanan

  • How to read outlook mail using java

    Hi,
    I am new to javamail ,
    I want to read outlook mail using java
    thanks
    Edited by: My_Problems on Jun 20, 2008 11:13 AM

    Hi!
    See Java Api Msgparser in GNU General Public License...
    A parser for .msg Files in Java :
    [http://auxilii.com/msgparser/|http://auxilii.com/msgparser/]
    Use POI... Apache POI - Java API To Access Microsoft Format Files...
    regards,

  • How to send Secured Mail using Java Mail?

    I want to send mails with "Send Secure" option using Java Mail. Now mails are being sent using Java Mail connecting to smtp host.
    Appreciating your help.
    Thanks.

    There are third party libraries to help with this. Bouncy Castle is very popular.
    See the [JavaMail Third Party Products|http://java.sun.com/products/javamail/Third_Party.html] page.

  • How to send e-mail using java mail api

    Hi can anyone tell me how to send mail using the javamail api, in short if possible an example code.

    Theres already lots of this code in the forums, use the search bar to the left.
    Also, there is a dedicated javaMail forum which you will find useful (you are currently in a JSP forum)
    http://forum.java.sun.com/forum.jspa?forumID=43

  • How do I send mails using java

    I am developing a web application. Is it possible to send E-mail and SMS using Java? If yes how I can do it?
    I want to send mail. How do I send?

    you can use javamail for the email part
    http://java.sun.com/products/javamail/
    there is a forum that will interest you about the SMS part
    http://forum.java.sun.com/thread.jspa?threadID=150292
    from discussjava.com
    Message was edited by:
    discussjava.com_

  • Want to send PDF as attachement using Java Mail

    HI,
    I am using Java mail API for sending PDF as attachment. Here is my sample code
    messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler("String data for PDF using iText", "text/plain" ));
    I am generating String for PDF file using iTEXT but I am unable to find out mimetype for passing into DataHandler as second paramete.
    Any idea?
    Thanks
    Shailesh

    Don't convert the data to String. It isn't text so
    you'll damage the binary content by doing that. In
    the "demos" directory of your JavaMail download
    you'll find a ByteArrayDataSource class you can use
    instead of a FileDataSource. Yes, this worked for me. I create the pdf in memory as as StringBuffer and I just changed the code from
    messageBodyPart.setDataHandler(new DataHandler(pdf.toString(), "application/pdf"));
    to
    messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(pdf.toString(), "application/pdf")));
    and it worked.
    Thanks a lot for your help,
    Dennis

  • Read mail using java, and set the mail

    How can I read mail and set message "SEEN" with java? (javax.mail)
    IMAP supports flags; so there are no problems, just using
    msg.setFlag(Flags.Flag.SEEN, true);
    but,
    POP3 does not support flags; how can I solve this in POP3? I have searched for this problem, found that I can just check for the header "Status", but this header does not exsist. Then I tried to add this header, but again, then I got IllegalWriteException cause pop3-msg is writeprotected (yes: I opened the folder READ_WRITE); so; does anyone know how I can solve this problem?

    Hi !
    Could u plz send me the code u r having for reading mails using IMAP...actually v r trying to read mails from outlook but due to some security reasons our team has decided not to use pop3 ...
    Thanks in Advance
    sha

  • Send attachment though mail using FM SO_NEW_DOCUMENT_ATT_SEND_API1

    Hello Gurus,
    Please help me with the mail sending attachment(excel file) using FM SO_NEW_DOCUMENT_ATT_SEND_API1.
    Here is the actual problem.
    I am using the FM SO_NEW_DOCUMENT_ATT_SEND_API1 in my prog.
    My attachment(excel sheet which is taken from internal table) contains the 10 records with one header description and data related to that description.
    I am able to send the attachment but the records in the attachment has more than 255 characters nearly 700 character length.
    So I spilt up each internal table record(with structure type solisti1) into 3 internal table records and populated it.
    It is showing the entire record in the attached file but I am not able to see some fields which are embeded deeply inside the cells.
    And they are not having any proper allignment.
    Please do help me.
    Promise to reward points.
    Mac

    Here is the sample code....
    DATA: OBJPACK LIKE SOPCKLSTI1 OCCURS  2 WITH HEADER LINE.
    DATA: OBJHEAD LIKE SOLISTI1   OCCURS  1 WITH HEADER LINE.
    DATA: OBJBIN  LIKE SOLISTI1   OCCURS 10 WITH HEADER LINE.
    DATA: OBJTXT  LIKE SOLISTI1   OCCURS 10 WITH HEADER LINE.
    DATA: RECLIST LIKE SOMLRECI1  OCCURS  5 WITH HEADER LINE.
    DATA: DOC_CHNG LIKE SODOCCHGI1.
    DATA: TAB_LINES LIKE SY-TABIX.
    Creating the document to be sent
    DOC_CHNG-OBJ_NAME = 'OFFER'.
    DOC_CHNG-OBJ_DESCR = 'Auction of a Picasso jr'.
    OBJTXT = 'Reserve price : $250000'.
    APPEND OBJTXT.
    OBJTXT = 'A reproduction of the painting to be auctioned'.
    APPEND OBJTXT.
    OBJTXT = 'is enclosed as an attachment.'.
    APPEND OBJTXT.
    DESCRIBE TABLE OBJTXT LINES TAB_LINES.
    READ TABLE OBJTXT INDEX TAB_LINES.
    DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).
    Creating the entry for the compressed document
    CLEAR OBJPACK-TRANSF_BIN.
    OBJPACK-HEAD_START = 1.
    OBJPACK-HEAD_NUM   = 0.
    OBJPACK-BODY_START = 1.
    OBJPACK-BODY_NUM   = TAB_LINES.
    OBJPACK-DOC_TYPE   = 'RAW'.
    APPEND OBJPACK.
    Creating the document attachment
    (Assume the data in OBJBIN are given in BMP format)
    OBJBIN = ' \O/ '. APPEND OBJBIN.
    OBJBIN = '     '. APPEND OBJBIN.
    OBJBIN = ' / \ '. APPEND OBJBIN.
    DESCRIBE TABLE OBJBIN LINES TAB_LINES.
    OBJHEAD = 'picasso.bmp'. APPEND OBJHEAD.
    Creating the entry for the compressed attachment
    OBJPACK-TRANSF_BIN = 'X'.
    OBJPACK-HEAD_START = 1.
    OBJPACK-HEAD_NUM   = 1.
    OBJPACK-BODY_START = 1.
    OBJPACK-BODY_NUM   = TAB_LINES.
    OBJPACK-DOC_TYPE   = 'BMP'.
    OBJPACK-OBJ_NAME   = 'ATTACHMENT'.
    OBJPACK-OBJ_DESCR = 'Reproduction object 138'.
    OBJPACK-DOC_SIZE   = TAB_LINES * 255.
    APPEND OBJPACK..
    Entering names in the distribution list
    RECLIST-RECEIVER = '<name>@yahoo.com'.
    RECLIST-REC_TYPE = 'U'.
    APPEND RECLIST.
    RECLIST-RECEIVER = 'DLI-NEUREICH'.
    RECLIST-REC_TYPE = 'P'.
    APPEND RECLIST.
    Sending the document
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
        EXPORTING
             DOCUMENT_DATA = DOC_CHNG
             PUT_IN_OUTBOX = 'X'
             COMMIT_WORK   = 'X'
        TABLES
             PACKING_LIST  = OBJPACK
             OBJECT_HEADER = OBJHEAD
             CONTENTS_BIN  = OBJBIN
             CONTENTS_TXT  = OBJTXT
             RECEIVERS     = RECLIST
        EXCEPTIONS
             TOO_MANY_RECEIVERS = 1
             DOCUMENT_NOT_SENT  = 2
                   OPERATION_NO_AUTHORIZATION = 4
                   OTHERS = 99.
         CASE SY-SUBRC.
           WHEN 0.
             WRITE: / 'Result of the send process:'.
             LOOP AT RECLIST.
               WRITE: / RECLIST-RECEIVER(48), ':'.
               IF RECLIST-RETRN_CODE = 0.
                 WRITE 'sent successfully'.
               ELSE.
                 WRITE 'not sent'.
               ENDIF.
             ENDLOOP.
           WHEN 1.
         WRITE:
         / 'no authorization to send to the specified number recipients!'.
           WHEN 2.
             WRITE: / 'document could not be sent to any of the recipient'.
           WHEN 4.
             WRITE: / 'no authorization to send !'.
           WHEN OTHERS.
             WRITE: / 'error occurred during sending !'.
         ENDCASE.
    Regards,
    Pavan.

  • Can you send encrypted e-mails using SO_NEW_DOCUMENT_ATT_SEND_API1

    We use SO_NEW_DOCUMENT_ATT_SEND_API1 to send e-mails to employees for various notifications.  The e-mails go out through SCOT and are sent to our lotus notes.
    We have a project being implemented, where the data we are sending in the note is kind of sensitive, so we'd like to see if we could send the e-mail as encrypted.   Does anyone know if there is some parameter we can set upon sending that would encrypt the text in the e-mail for sending?

    FORM pssword_prot_encryptpdf .
      DATA: BEGIN OF command_list OCCURS 0.
              INCLUDE STRUCTURE sxpgcolist.
      DATA: END OF command_list .
      DATA: BEGIN OF exec_protocol OCCURS 0.
              INCLUDE STRUCTURE btcxpm.
      DATA: END OF exec_protocol.
      DATA: status LIKE btcxp3-exitstat,
      commandname LIKE sxpgcolist-name VALUE 'ZTEST',
      sel_no LIKE sy-tabix.
    * GET LIST OF EXTERNAL COMMANDS
      CALL FUNCTION 'SXPG_COMMAND_LIST_GET'
        EXPORTING
          commandname     = commandname
          operatingsystem = sy-opsys
        TABLES
          command_list    = command_list
        EXCEPTIONS
          OTHERS          = 1.
      CALL FUNCTION 'SXPG_COMMAND_CHECK'
        EXPORTING
          commandname                = command_list-name
          operatingsystem            = sy-opsys
        EXCEPTIONS
          no_permission              = 1
          command_not_found          = 2
          parameters_too_long        = 3
          security_risk              = 4
          wrong_check_call_interface = 5
          x_error                    = 6
          too_many_parameters        = 7
          parameter_expected         = 8
          illegal_command            = 9
          communication_failure      = 10
          system_failure             = 11
          OTHERS                     = 12.
      CLEAR command_list.
      REFRESH command_list.
      DATA: v_dir_input      TYPE sxpgcolist-parameters.
      DATA: v_dir_input1      TYPE sxpgcolist-parameters.
      break developer.
      command_list-name = 'ZTEST'.
      command_list-opsystem = 'Windows NT'.
      CONCATENATE   'd:\pdf\' main_dtl-vbeln '.PDF' INTO name.
      CONCATENATE 'cmd /c d:\pdf\encryptpdf.exe' '-i' name  '-o ' name  '-u'  hdr-password INTO v_dir_input SEPARATED BY space .
      READ TABLE command_list INDEX sel_no.
      break developer.
      CONCATENATE command_list-opcommand v_dir_input INTO command_list-opcommand SEPARATED BY space.
    * CHECK AUTHORIZATION
      command_list-addpar = 'X'.
      APPEND command_list.
      CONSTANTS: c_extcom    TYPE sxpgcolist-name VALUE 'ZTEST',
                 c_oper      TYPE syopsys VALUE 'Windows NT'.
      DATA: t_result         TYPE STANDARD TABLE OF btcxpm.
      v_dir_input  =  command_list-opcommand.
      CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
        EXPORTING
          commandname                   = c_extcom
          additional_parameters         = v_dir_input
          operatingsystem               = c_oper
        TABLES
          exec_protocol                 = t_result
        EXCEPTIONS
          no_permission                 = 1
          command_not_found             = 2
          parameters_too_long           = 3
          security_risk                 = 4
          wrong_check_call_interface    = 5
          program_start_error           = 6
          program_termination_error     = 7
          x_error                       = 8
          parameter_expected            = 9
          too_many_parameters           = 10
          illegal_command               = 11
          wrong_asynchronous_parameters = 12
          cant_enq_tbtco_entry          = 13
          jobcount_generation_error     = 14
          OTHERS                        = 15.
    ENDFORM.                    " PSSWORD_PROT

  • How to send a string from sender to receiver side using java

    I am doing a project on Digital Signature.I have already done with the GUI using java swing.
    Now i want to send a string from the sender side to the receiver side on the click of a button using socket programming.
    Please can anybody provide me with the code as early as possible.

    http://catb.org/~esr/faqs/smart-questions.html

Maybe you are looking for

  • IMac to TV (HDMI) : Ghost display when TV is off

    Hi,  I'm using my Sony TV (40BX420) as my secondary display for my iMac 27", connected via MiniDisplay Adapter+HDMI Cable.   When connected everything works fine, the audio & picture appears normally.  I use this setup to watch movies on my bigger sc

  • Accounting Document Number range error

    Dear All, As per my client requirement 3 types of billing document is thier. SD Consulant maintain the number range below 1. Domestic billing (Number range between 000040000- 0000401000 ) 2. Export billing (Number range between  0000900001-0000901000

  • Reporting Agent and Process chain

    Hi All, We have requirement where every day we need to print some BW reports. For this we have created reporting agent. But when we execute reports manually through reporting agent it works fine. But if we include reporting agent in process chain it

  • On AOL, how can I forward an e-mail using my contacts

    I am no longer using AOL's web site, but was told to get to AOL thru Firefox. How can I forward my e-mails? When I hit "forward" the e-mail appears, but when I hit my "contacts, (so I can insert e-mail addresses) the e-mail is gone and now is in "old

  • Urgent : IDOC posting problem

    Dear All , Currently I am working on IDOC - XI- FILE scenario . I have created a customized idoc and with the help of customized program I have uploaded the value in IDOC . Now my query is how XI will take IDOC : Option 1 : Do I need to trigger IDOC