使用mail.jar和activation.jar发送所需的邮件内容

发布于 2024-09-28 18:34:47 字数 259 浏览 7 评论 0原文

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

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

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

请告诉我如何将大文本添加到邮件内容中。任何类型的链接或教程来证明其合理性都将非常值得赞赏。提前非常感谢..祝大家周日快乐..!!

Am using mail.jar and activation.jar as classpath and have programmed a automatic mail sending and it works fine.

In my program, the content is declared as a String. But my requirement is, I need to retrieve few of the counts from different tables of my SQL DB and attach the same in my content of the mail.

I think declaring the content as String will not help me out to achieve the task since the number of lines that I will be sending in the content of the mail will be more than five or six.

Kindly let me know how a large text can be added to the content of the mail. Any sort of links or tutorials for justifying the same will be highly appreciable. Thanks a lot in advance.. Happy Sunday guys.. !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2024-10-05 18:34:47

您可能熟悉 System.out.println 等...您可以使用此方法打印到如下字符串:

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 中) out.println-call) print:

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-方法调用轻松构建电子邮件消息字符串。

You're probably familiar with System.out.println etc... You can use this method to print to a string like this:

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);

The above snippet of code would (in the last System.out.println-call) print:

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

This way you could easily construct an email message string using println-method calls.

粉红×色少女 2024-10-05 18:34:47

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

实际上假设您的邮件正文是纯文本格式,您应该使用 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";

If you simply need to send a few lines instead of one, you can still use a single String. A String can hold multiple lines of text. Just add newlines where necessary.

Actually assuming your mail body is in plain text format, you should use CR LF as a line terminator (\r\n at the end of each line).

So you can build your content like this:

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";
深海少女心 2024-10-05 18:34:47

查看 [链接文本][1] 以动态地将参数与字符串合并。您可以考虑将文本作为资源从类路径加载,而不是静态字符串。

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

Check out [link text][1] for dynamically merging parameters with a String. Instead of a static String you could consider loading the text as a resource from the classpath.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文