Reputation: 2463
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.. !!
Upvotes: 1
Views: 2213
Reputation: 421100
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.
Upvotes: 1
Reputation: 2413
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...)
Upvotes: 1
Reputation: 21995
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";
Upvotes: 1