cancel
Showing results for 
Search instead for 
Did you mean: 

to format data in java mail message

Former Member
0 Kudos

HI,

In my application ,I am triggering a javamail which contain 5 fields in it....

is there by any means ,I can Format the data appearence in the mail message..(like inserting table )

so that that appears in the mail received by the end user,..

Thanx,

Arjun.G

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

change the mime type of the mail to html and make use of table tags available in html.

Regards

Ayyapparaj

Former Member
0 Kudos

hi,

Can you please help me to change the mime type of the mail...

a code snippet can help us a lot...

Thanx..

Arjun.G

Former Member
0 Kudos
Former Member
0 Kudos

Hi,

I am not able to import the necessary imports for

ByteArrayDataSource

for the code that u gave in the link....

thanx,

Arjun.G

Former Member
Former Member
0 Kudos

Hi,

We are not able to add the import of javax.mail.util...in our eclipse..i am not finding it...

do i need to import any libraries for it / or should i extend any variable to import it..

Thanx.

Arjun.G

chintan_virani
Active Contributor
0 Kudos

Arjun,

Using the JavaMail API requires the JavaMail and JavaBeans Activation Framework JAR files i.e. mail.jar and activation.jar respectively to be in the CLASSPATH of the project.

So download JavaMail 1.4.1 from following [link|http://java.sun.com/products/javamail/downloads/index.html]

and JAF from [here|http://java.sun.com/products/javabeans/jaf/downloads/index.html]

Extract the jars I mentioned from the zip files onto your hard-drive somewhere and then add them to your project classpath by doing Right click on your project --> Properties --> Java Build path --> Add External JARs

Check this [Sun Javamail Link|http://java.sun.com/products/javamail/index.jsp] for more details.

Chintan

Answers (2)

Answers (2)

Former Member
0 Kudos

df

Former Member
0 Kudos

Hi,

This is the code i have written for formatting the mail in the java mail...

It does not work out...

Error :

The initial exception that caused the request to fail, was:

java.lang.NoClassDefFoundError: javax/mail/util/ByteArrayDataSource

at com.sap.admin.component.Emp_TrainingNeeds.collect(Emp_TrainingNeeds.java:253)

at com.sap.admin.component.Emp_TrainingNeeds.onActionSaveNExit(Emp_TrainingNeeds.java:380)

at com.sap.admin.component.wdp.InternalEmp_TrainingNeeds.wdInvokeEventHandler(InternalEmp_TrainingNeeds.java:255)

at com.sap.tc.webdynpro.progmodel.generation.DelegatingView.invokeEventHandler(DelegatingView.java:87)

at com.sap.tc.webdynpro.progmodel.controller.Action.fire(Action.java:67)

... 27 more



//Java Mail Code Starts from Here		
		

//  BufferedWriter wr = new BufferedWriter(new InputStream());			  

String mess = wdContext.currentNeedBeanElement().getCategory().concat("|").concat(wdContext.currentNeedBeanElement().getCoursegroup().concat("|").concat(wdContext.currentNeedBeanElement().getCoursegroup().concat("|").concat(wdContext.currentNeedBeanElement().getBywhen().toString()).concat("|").concat(wdContext.currentNeedBeanElement().getPriority().concat("|").concat(wdContext.currentNeedBeanElement().getRemarks())
)));
char[] mes= mess.toCharArray();

//InputStream s = new InputStream();
///StringReader s = new StringReader(mess);
InputStream is = new ByteArrayInputStream(mess.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
//StringReader in1 =  new StringReader(mess);



wdContext.currentContextElement().setTextMessage(mess);
	String host ="202.54.xxx.xxx "; // Specify the host
//		   Get the From Address
//		String from = wdContext.currentContextElement().getFromAddress();
		String from ="aaaa@yyyy.com";
//		   Get the To Address
//		String to = wdContext.currentContextElement().getToAddress();
		String to=" xxxx@yyyy.com";
//		   Initialize Session
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
	
		Session session = Session.getDefaultInstance(props, null);
//		   Create new MimeMessage
		MimeMessage message = new MimeMessage(session);
		
		IWDMessageManager messageMgr = wdControllerAPI.getComponent().getMessageManager();
		try {// Set the From Address
			message.setFrom(new InternetAddress(from));
//			   Set the To Address
			message.addRecipient(Message.RecipientType.TO,
			new InternetAddress(to));
//			   Set the Subject
String subj = wdContext.currentNeedBeanElement().getUpdatedby();
String sub = "Training Need of ".concat(subj); 
			message.setSubject(sub);
			
			collect(in, message);
			
			message.setHeader("X-Mailer","sendhtml");
			message.setSentDate(jsqlD);
//			   Set the Text
			message.setText(wdContext.currentContextElement().getTextMessage());
			
//			   Send message
			Transport.send(message);
			
			messageMgr.reportSuccess("Mail Sent Successfully");
			} catch (AddressException e) {
			messageMgr.reportException(e.toString(),false);
			} catch (MessagingException e) {
			messageMgr.reportException(e.toString(),false);
			}
			


 public void collect( java.io.BufferedReader in, javax.mail.Message message )
  {
    //@@begin collect()
    
   try{
   
	String line;
		String subject = message.getSubject();
		StringBuffer sb = new StringBuffer();
		sb.append("<HTML>\n");
		sb.append("<HEAD>\n");
		sb.append("<TITLE>\n");
		sb.append(subject + "\n");
		sb.append("</TITLE>\n");
		sb.append("</HEAD>\n");

		sb.append("<BODY>\n");
		sb.append("<H1>" + subject + "</H1>" + "\n");
//	String mess = wdContext.currentNeedBeanElement().getCategory().concat("|").concat(wdContext.currentNeedBeanElement().getCoursegroup().concat("|").concat(wdContext.currentNeedBeanElement().getCoursegroup().concat("|").concat(wdContext.currentNeedBeanElement().getBywhen().toString()).concat("|").concat(wdContext.currentNeedBeanElement().getPriority().concat("|").concat(wdContext.currentNeedBeanElement().getRemarks())
//				)));
//		sb.append(mess);
		
		while (( line = in.readLine()) != null) {
			
			sb.append(line);
			sb.append("\n");
		}
		sb.append("</BODY>\n");
		sb.append("</HTML>\n");

		message.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html")));
   }catch(MessagingException e){
   	e.getMessage();
   	e.printStackTrace();
   	}catch(IOException e){
   		e.getMessage();
   		e.printStackTrace();
   	}
    //@@end
  }

Thxs

Arjun.G