我正在创建一个 Java 应用程序,我想添加的功能之一是向用户发送生成的电子邮件。我已经在我的 Macbook 上设置了邮件,我可以很好地从命令行发送电子邮件。我在调用 runtime.exec() 时发送电子邮件时遇到问题。任何人都知道为什么它不会执行和发送电子邮件?
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("echo This is the body | mail -s Subject -F myemail@gmail.com");
}
catch ( Exception e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我没有收到任何错误,一切都编译正常。我只是没有收到任何电子邮件。如果有人可以帮助,将不胜感激。
最佳答案
Runtime.exec(String)
不会通过将整个字符串粘贴到 UNIX shell 并运行来执行整个字符串。这是因为这是一个巨大的安全漏洞。
假设您这样做了:
runtime.exec("Hello "+name+" | mail ...");
然后我可以将 name
设置为 ; rm -rf/*; echo
或 && cat/etc/passwd
,或其他一些 shell 注入(inject)代码。
所以,Java is breaking up your command into parts :
More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.
最终,您将单独运行 echo
命令,并使用 |
和 mail
等参数。 echo
命令只会将这些打印到标准输出,您不会收集这些内容。它永远不会调用 mail
命令。
由于涉及安全风险,您不应使用 mail
命令从 Java 发送邮件。您应该使用 JavaMail 包,它提供了一个安全方便的 API 来发送邮件。 UNIX mail
命令通过连接到在本地计算机上运行的 sendmail
守护进程的端口 25 上工作,JavaMail 也可以这样做。
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendMail {
public static sendMail(String from, String to, String subject, String body)
throws MessagingException
{
final Properties p = new Properties();
p.put("mail.smtp.host", "localhost");
final Message msg = new MimeMessage(Session.getDefaultInstance(p));
msg.setFrom(new InternetAddress(from));
msg.addRecipient(RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
}
}
关于Java Runtime.exec() 不从命令行发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24680759/