2011-12-28 66 views

回答

3

您可以將圖像轉換爲字節數組,然後使用以下方法將該文件作爲附件發送。

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

這可能是幫助你檢查它

//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()); 
} 

這是圖像文件

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); 
}