1

由于我的 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”的附件。

4

1 回答 1

3

编辑:用正确的解决方案替换愚蠢的东西

大多数 Linux 发行版中的mailx命令是heirloom mailx。它比原来的 BSD mailx 做了更多的事情,如果其中有任何不可打印的字符,它会自动对其输入进行编码*。

这里的问题是它认为\r字符是非标准的,因此它在邮件中添加了以下标题:

Content-Type: application/octet-stream
Content-Transfert-Encoding: base64

并且邮件的文本实际上是基于 64 位编码的。它不是真正的附件,但许多邮件阅读器将此类邮件视为带有未命名附件的空正文。

所以解决方案是\r从邮件正文中删除所有内容。

事实上,如果您的LANG环境变量声明了一个可以使用非 7 位字符(éèûôüö ...)的语言环境,mailx 似乎足够聪明地声明一个扩展字符集(对于 fr 语言环境是 ISO-8859-1)并执行引用打印编码。因此,即使邮件中有(至少是西欧)非 7 位 ASCII 字符,只要没有控制字符,邮件也应该正常发送。

最后机会的解决方案是不使用 mailx 并直接使用 sendmail。

于 2014-06-16T21:21:53.917 回答