aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes How to send email in text format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to send email in text format?" Watch "How to send email in text format?" New topic
Author

How to send email in text format?

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hello Everybody,

I have a .csv file, in which details about emp-name, salary and email address are stored.
My application reads this .csv file, and sends a mail to respective employee with his id, salary and other details.

My problem: The mail sent to each employee is in html format. But, I want to send an e-mail in text format. (a .txt file as an attachment will do) I tried changing content type to text. Then, I got following error:

Error:
e=javax.mail.internet.ParseException

Code:
------------------------------------
try
{String s1="";
File f1 = new File(way2);

FileInputStream fstream = new FileInputStream(f1); //new

BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

int count=0;
//while((eachRow() = br.readLine()) != null && eachRow.trim().length() > 0){

while((s1=br.readLine())!=null )
{
count++;
//out.println("within while loop "+count);
StringTokenizer st = new StringTokenizer(s1,",");
/*int z=s1.eachRow.trim().length();
out.println("Loop"+count+" "+"Length="+z);*/
if ((st.hasMoreTokens())&&(count>1))
{
String a=st.nextToken().trim();
String b=st.nextToken();
String c=st.nextToken();
String d=st.nextToken();
String e=st.nextToken();
String f=st.nextToken();
String g=st.nextToken();
String h=st.nextToken();
String i=st.nextToken();
String j=st.nextToken();
String k=st.nextToken();
String l=st.nextToken();
String m=st.nextToken();
String n=st.nextToken();
String o=st.nextToken();
String p=st.nextToken();
String q=st.nextToken();
String mail=st.nextToken();
String s=st.nextToken();
%>

</tr>
<!--Creating a user defied function to send the mail for each mail id and calling it here.-->
<%
String to =mail;
String from =request.getParameter("fromadd");
String subject ="Employee details";
String smtp ="mail.xxxxxxxxx.com";
String blank=" ";
String message="<html>";
out.println(message);
message=message.concat("<head>");
message=message.concat("</head>");
message=message.concat("<body>");
message=message.concat("<H2 align=center>");
message=message.concat("<FONT COLOR=red>");
message=message.concat("Emplyoee details");
message=message.concat("</H2>");
message=message.concat("</FONT>");
message=message.concat("<TABLE WIDTH=100% COLS=19 BORDER=1>");
message=message.concat("<TD><FONT size=3 COLOR=blue><b>EMP ID</TD>");
message=message.concat("<TD> Name</TD>");
message=message.concat("<TD>Dept. No.</TD>");
message=message.concat("<TD>Acc.No.</TD>");
message=message.concat("<TD>Bank</TD>");
message=message.concat("<TD>GFNO</TD>");
message=message.concat("<TD>Sales</TD>");
message=message.concat("<TD>LOCTRAVL</TD>");

message=message.concat("<TD>Subspn</td>");
message=message.concat("<TD>Entmnt</TD>");
message=message.concat("<TD>Incentieve</TD>");
message=message.concat("<TD>Conf</TD>");
message=message.concat("<TD>Mobile</TD>");
message=message.concat("<TD>Others</TD>");
message=message.concat("<TD>Advance</td>");
message=message.concat("<TD>Ticketamt</TD>");
message=message.concat("<TD>Net</td>");
message=message.concat("</td>Rem</TD>");
message=message.concat("</b></font></TR>");
message=message.concat("<TR>");
message=message.concat("<TD><FONT size=2>");
message=message.concat(a);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(b);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(c);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(d);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(e);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(f);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(g);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(h);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(i);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(j);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(k);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(l);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(m);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(n);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(o);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(p);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(q);
message=message.concat("</TD>");
message=message.concat("<TD>");
message=message.concat(s);
message=message.concat("</b></font></TD>");
message=message.concat("</TR>");
message=message.concat("</TR>");
message=message.concat("</TABLE>");
message=message.concat("</body>");
message=message.concat("</html>");
//String result="";
Properties props = System.getProperties();
// Puts the SMTP server name to properties object
props.put("mail.smtp.host", smtp);
// Get the default Session using Properties Object
Session session1 = Session.getDefaultInstance(props, null);
// Create a New message
MimeMessage msg = new MimeMessage(session1);
// Set the From address
msg.setFrom(new InternetAddress(from));
// Setting the "To recipients" addresses
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
/* // Setting the "cc recipients" addresses
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
// Setting the "Bcc recipients" addresses
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc, false)); */
// Sets the Subject
msg.setSubject(subject);
// set the meaasge in Text format
msg.setContent(message,"text");
// Set the Date: header
msg.setSentDate(new java.util.Date());
// Send the message
Transport.send(msg);
// Display Success message
result =result.concat("<tr><td>"+b+"</td>"+"<td>"+to+"</td></tr>");
}//end of if of hasmore element
}// end of while loop
out.println(result);
%>
</table>
<%/*br.close();
fstream.close();*/
/*if(f1.delete())
{
out.println("<table><tr></tr><tr></tr><tr></tr><tr><td><font color=brown><i><b>After sending the mail, file is deleted from Tomcat Server</b></i></font></td></tr></table>");
//System.out.println("After sending the mail, file is deleted from Tomcat Server");
} */
}catch(Exception e)
{
// If here, then error in sending Mail. Display Error message.
result="Unable to send your message";
out.println("e="+e);
//System.out.println("There is an error,Check your program");%>
<h3><% out.println("SORRY, There is an error in mail delivery. Please Check your input.");%></h3>
<a href="previous.jsp"><h2>Back</h2></a>
<% }
----------------------------------------------
Any help will be appreciated.

Thanks in advance,
regards
Ashvini


With Regards,
Anu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Moved to Other Java APIs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

You should set the content type to the MIME type "text/plain":

msg.setContent(message, "text/plain");

Not to just "text".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thank you Jesper.

The code is working fine. But, i am finding alignment problem. IF employee name is too big, other columns will shift to right and data will not be exactly under column header. (it is going in zig zag way)

I thought sending text message as an attachment will be the best solution to this.
Any help will be greatly appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
and sends a mail to respective employee with his id, salary and other details.


You are sending an email that contains salary information? That is a terrible idea from a privacy point of view. How do you guarantee that no one else sees that? What happens if an email bounces - suddenly an administrator may have access to it? What if an email address is mistyped, but still valid? What if someone has opened their email read-only to someone else during vacation? All the same reasons why you don't send login information to systems that contain sensitive information by email apply here as well. I strongly urge you to reconsider this idea.


Ping & DNS - my free Android networking tools app
 
 
subject: How to send email in text format?