由于我的 SMTP 提供商对一天内可以发送的电子邮件数量有限制,我编写了一个 Java 代码来调用 Linux 系统的“mailx”,我的 java 程序正在运行。
这是该代码:
package sys.cmd;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class IntermediateJavaLinuxMailX {
public static void main(String[]args) throws IOException{
email(
new ArrayList<String>(){{
add("myemailid@myserver.com");
add("myothermailid@otherserver.com");
}},
"Error Message",
"Hello World!\r\n This is message"
);
}
public static void email(
List<String>toEmailIds,
String subject,
String msgText
) throws IOException{
String toEmails = toString(toEmailIds);
String[]args=new String[]{"/bin/sh" , "-c", "mailx -s \""+subject+"\" "+toEmails};
System.out.println("The command for bash is: "+args[2]);
Process proc= Runtime.getRuntime().exec(args);
OutputStream o = proc.getOutputStream();//probable output for text
InputStream i = new ByteArrayInputStream(msgText.getBytes());//probable input for message-text
read2end(i, o);
o.close();
}
private static String toString(List<String> toEmailIds) {
StringBuilder sb= new StringBuilder();
for(String toEmailId:toEmailIds){
sb.append(toEmailId).append(' ');
}
return sb.toString();
}
private static void read2end(InputStream i, OutputStream o) throws IOException {
byte[]b=new byte[1000];
for(int a=0;(a=i.read(b))>-1;)
o.write(b, 0, a);
i.close();
}
}
问题是:在收件人端收到的电子邮件,文本不在邮件正文中,但在名为“noname”的附件文件中。
问题是:如何使字符串msgText
出现在电子邮件的消息正文中。
添加我到目前为止所做的另一件事:
我写了另一个代码,它使用临时文件来存储消息文本,然后使用文件重定向(<
)来添加消息文本,它给出了预期的结果。但这是一种间接的方式。有没有直接的方法?这是另一个代码:
public static void email(
List<String>toEmailIds,
List<String>ccEmailIds,
List<String>bccEmailIds,
String subject,
byte[][]attachContents,
String messageText
) throws IOException{
String toEmails=toString(" " , toEmailIds,' ');
String ccEmails=notEmpty(ccEmailIds)?toString(" -c ", ccEmailIds,','):"";
String bcEmails=notEmpty(bccEmailIds)?toString(" -b ", bccEmailIds,','):"";
String recip=bcEmails+ccEmails+toEmails;
String[]attachmentTempFiles=new String[notEmpty(attachContents)?attachContents.length:0];
String attachFilePaths="";
for(int x = 0;x<attachmentTempFiles.length;++x){
String attachTempPath = "/path/temp/attach_"+x+".file";
byteArray2File(attachContents[x],attachTempPath);
attachmentTempFiles[x]=" -a "+attachTempPath;
attachFilePaths+=attachmentTempFiles[x];
}
String msgTxtTempFilePath="/path/temp/msg.txt";
byteArray2File(messageText.getBytes(), msgTxtTempFilePath);
msgTxtTempFilePath=" < "+msgTxtTempFilePath;
String mailxCommand = "mailx " + attachFilePaths + " -s \"" + subject +"\" "+ recip + msgTxtTempFilePath;
Runtime.getRuntime().exec(new String[]{"/bin/sh" , "-c", mailxCommand});
}
private static void byteArray2File(byte[] bs, String path) throws IOException {
FileOutputStream fos=new FileOutputStream(path);
ByteArrayInputStream bais=new ByteArrayInputStream(bs);
read2end(bais, fos);
fos.close();
}
private static boolean notEmpty(byte[][] bs) {
return bs!=null && bs.length>0;
}
private static boolean notEmpty(List<String> strings) {
return strings!=null && !strings.isEmpty();
}
private static String toString(String pre, List<String> toEmailIds,char separator) {
StringBuilder sb= new StringBuilder(pre);
for(String toEmailId:toEmailIds){
sb.append(toEmailId).append(separator);
}
return sb.substring(0,sb.length()-1);
}
private static void read2end(InputStream i, OutputStream o) throws IOException {
byte[]b=new byte[1000];
for(int a=0;(a=i.read(b))>-1;)
o.write(b, 0, a);
i.close();
}
--Edit-- 在@Serge Ballesta 的评论之后添加:
“嗯,我试着用谷歌搜索,发现通过管道传输到 Linux mailx 的纯文本文件变成了“Content-Type: application/octet-stream”(附件)。你的问题会不会是一样的?可以肯定的是你能控制收到的消息?”
这段代码也有同样的效果:
email(
new ArrayList<String>(){{add("user_abc@mail1.com");add("person-xyz@mailer2.com");}},
"Error Message",
"Content-Type: text/plain; charset=us-ascii\r\n" +
"Content-Disposition: inline\r\n\r\n" +
"Hello World!\r\n" +
"This is message.\r\n\r\n\r\n"
);
尽管如此,所有的消息文本都会进入一个名为“noname”的附件。