2011-12-28 11 views

答えて

3

イメージをバイト配列に変換し、次の方法を使用してファイルを添付ファイルとして送信できます。これは、ヘルプは、あなたが、これは画像ファイルです

//create a multipart 
Multipart mp = new Multipart(); 

//data for the content of the file 
String fileData = "<html>just a simple test</html>"; 
String messageData = "Mail Attachment Demo"; 

//create the file 
SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html",fileData.getBytes()); 

TextBodyPart tbp = new TextBodyPart(mp,messageData); 

//add the file to the multipart 
mp.addBodyPart(tbp); 
mp.addBodyPart(sap); 

//create a message in the sent items folder 
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT); 

Message message = new Message(folders[0]); 

//add recipients to the message and send 
try { 
    Address toAdd = new Address("[email protected]","my email"); 
    Address toAdds[] = new Address[1]; 
    toAdds[0] = toAdd; 
    message.addRecipients(Message.RecipientType.TO,toAdds); 
    message.setContent(mp); 

    Transport.send(message); 
} catch (Exception e) { 
    Dialog.inform(e.toString()); 
} 

それをチェックすることも

public synchronized boolean sendMail(final byte []data) 
     {  
     Folder[] folders = store.list(4); 
     Folder sentfolder = folders[0]; 
     // create a new message and store it in the sent folder 
     msg = new Message(sentfolder); 
     multipart = new Multipart(); 
     textPart = new TextBodyPart(multipart,"Image"); 
     Address recipients[] = new Address[1]; 
     try { 

       recipients[0] = new Address(address, "XYZ"); 
       msg.addRecipients(Message.RecipientType.TO, recipients); 
       msg.setSubject("Image"); 
       try { 
         Thread thread = new Thread("Send mail") { 
           public void run() { 
             try { 


                attach = new SupportedAttachmentPart(
                   multipart, "application/octet-stream", 
                   "title",data);              


               multipart.addBodyPart(textPart); 
               multipart.addBodyPart(attach); 
               msg.setContent(multipart); 
               Transport.send(msg); 
             } 
             catch(SendFailedException e) 
             { 

             } 
             catch (final MessagingException e) { 


             } 
             catch (final Exception e) { 

             } 

           }   
         }; 
         thread.start(); 
         return true; 
       } 
       catch (final Exception e) 
       { 

       } 
     }catch (final Exception e) { 

     } 

     return false; 
} 
0

InputStream inputStream; 

     FileConnection fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); 
     if(fconn.exists()){ 
      inputStream=fconn.openInputStream(); 
      byte[] data = IOUtilities.streamToBytes(inputStream); 

      inputStream.close(); 
      fconn.close();     
      Multipart multipart = new Multipart(); 

      SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart, ".txt/.jpeg", "attachment1", data); 

      multipart.addBodyPart(attach); 
} 
関連する問題