Java 使用mail.jar和activation.jar发送所需邮件内容
我使用mail.jar和activation.jar作为类路径,并编写了一个自动邮件发送程序,运行良好 在我的程序中,内容被声明为字符串。但我的要求是,我需要从SQL DB的不同表中检索一些计数,并将它们附加到邮件内容中 我认为将内容声明为字符串不会帮助我完成任务,因为我将在邮件内容中发送的行数将超过五六行Java 使用mail.jar和activation.jar发送所需邮件内容,java,email,activation,Java,Email,Activation,我使用mail.jar和activation.jar作为类路径,并编写了一个自动邮件发送程序,运行良好 在我的程序中,内容被声明为字符串。但我的要求是,我需要从SQL DB的不同表中检索一些计数,并将它们附加到邮件内容中 我认为将内容声明为字符串不会帮助我完成任务,因为我将在邮件内容中发送的行数将超过五六行 请让我知道如何将大文本添加到邮件内容中。任何类型的链接或教程证明同样的将是非常可观的。先谢谢你。。周日快乐,伙计们 如果您只需要发送几行而不是一行,您仍然可以使用单个字符串。一个字符串可以容
请让我知道如何将大文本添加到邮件内容中。任何类型的链接或教程证明同样的将是非常可观的。先谢谢你。。周日快乐,伙计们 如果您只需要发送几行而不是一行,您仍然可以使用单个字符串。一个字符串可以容纳多行文本。只要在必要的地方添加新行 实际上,假设您的邮件正文是纯文本格式,您应该使用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";
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
等。。。您可以使用此方法打印到如下字符串:
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";
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
-call中)打印:
通过这种方式,您可以使用
println
-方法调用轻松构建电子邮件消息字符串。查看[link text][1]以动态地将参数与字符串合并。代替静态字符串,可以考虑将文本加载为类路径中的资源。
[1] :,java.lang.Object…谢谢您的解释。我想我可以用这个。是否可以使用邮件内容中的字符串形成一个包含三行六列的表?是的。如果我需要列出一个表格,我肯定会选择
printf
。在web上有任何例子可以用来定义相同的表格吗?非常感谢。。你让事情看起来更容易