1

我使用 mail.jar 和 activation.jar 作为类路径,并编写了自动邮件发送程序,它工作正常。

在我的程序中,内容被声明为字符串。但我的要求是,我需要从我的 SQL DB 的不同表中检索一些计数并将其附加到我的邮件内容中。

我认为将内容声明为字符串不会帮助我完成任务,因为我将在邮件内容中发送的行数将超过五六行。

请让我知道如何将大文本添加到邮件内容中。任何类型的链接或教程来证明这一点都是非常可观的。提前非常感谢.. 祝大家星期天快乐.. !!

4

3 回答 3

1

如果您只需要发送几行而不是一行,您仍然可以使用单个字符串。一个字符串可以包含多行文本。只需在必要时添加换行符。

实际上假设您的邮件正文是纯文本格式,您应该使用 CR LF 作为行终止符(\r\n在每行的末尾)。

所以你可以像这样构建你的内容:

String content = "This is line 1 of the email\r\n"
    + "This is line 2 of the email\r\n"
    + "This is line 3 of the email\r\n";
于 2010-10-24T07:34:26.300 回答
1

您可能熟悉System.out.printlnetc... 您可以使用此方法打印到这样的字符串:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

pw.println("Hello");            // Appends two
pw.println("World");            // separate lines

pw.printf("Hello %d World", 5); // Using printf

pw.println();                   // appends a new-line
pw.print("Another line.");      // appends string w/o new-line

pw.println();                   // appends two
pw.println();                   // newlines

String rowFormat = "%8s %8s %8s %8s %8s%n";
pw.printf(rowFormat, "Col A", "Col B", "Col C", "Col XY", "Col De", "Col Ef");
pw.printf(rowFormat, "A", "19", "Car", "55", "Blue", "Last");
pw.printf(rowFormat, "X", "21", "Train C", "-4", "Red", "Demo");
pw.printf(rowFormat, "B", "-9", "Bike", "0", "Green", "Column");

String message = sw.toString();

System.out.println(message);

上面的代码片段将(在最后一次System.out.println调用中)打印:

Hello
World
Hello 5 World
Another line.

   Col A    Col B    Col C   Col XY   Col De
       A       19      Car       55     Blue
       X       21  Train C       -4      Red
       B       -9     Bike        0    Green

这样,您可以使用println-method 调用轻松构建电子邮件消息字符串。

于 2010-10-24T07:42:04.803 回答
1

查看 [link text][1] 以使用字符串动态合并参数。您可以考虑将文本作为类路径中的资源加载,而不是静态字符串。

[1]: http: //download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#format (java.lang.String, java.lang.Object...)

于 2010-10-24T07:43:30.063 回答