how to attach an output of a java program to the email

Hello all,

pl help me.I've written a mail program.I want to send a unique key with it.I've written a key generation program.Now I want the output of the key generation program to be attached to the mail.How to do it?I don't want to send it in attachments.The key will be unique for each users

[308 byte] By [erikaa] at [2007-11-26 13:34:21]
# 1

Take that key generation program and turn it into a key generation class, with a method named something like getKey(). Then in your mail sending program, call the getKey() method and assign it to a variable in your program. Take that variable and do whatever you need to do to put it into the message. (It's hard to be specific without knowing anything about the structure of your keys.)

DrClapa at 2007-7-7 22:15:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thaks for ur reply iam using a HMAC _MD5 algorthim for generating key.i need to attach the key generated with my mail message so i attch my codeing with this mail u pls help me with this issuse.

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.Serializable;

import java.io.UnsupportedEncodingException;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.Security;

import com.sun.net.ssl.*;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.crypto.KeyGenerator;

import javax.crypto.Mac;

import javax.crypto.SecretKey;

public class Jmail extends Eval{

private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";

private String SMTP_AUTH_USER = "xxax";

private String SMTP_AUTH_PWD = "sdsa";

//private static final String emailMsgTxt= "Online Order Confirmation Message. Also include the Tracking Number.";

private static final String emailSubjectTxt = "Order Confirmation Subject";

private static final String emailFromAddress = "xxxxxx@yahoo.com";

private static String emailMsgTxt = "Dear ABC"

+" Thank you for your interest for our product.Your evaluation key is"

+" Feel free to communicate us if you encounter any problem in evalation. "

+ "Your successfull evaluation is important to us."

+"Sincerely xxxxx"

+" Click the link to start using product ";

private static final String[] emailList = { "xxxxx@yahoo.co.in","fxxx@yahoo.com"};

public static void main(String args[]) throws Exception

{

Jmail smtpMailSender = new Jmail();

Jmail dd=new Jmail();

smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);

System.out.println("Sucessfully Sent mail to All Users");

dd.keygenerator();

}

public void postMail( String recipients[ ], String subject,

String message , String from) throws MessagingException, InvalidKeyException, NoSuchAlgorithmException, FileNotFoundException, IOException

{

boolean debug = true;

Properties props = new Properties();

props.put("mail.smtp.host", SMTP_HOST_NAME);

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.port", "25");

props.put("mail.smtp.protocol","smtp");

props.put("mail.debug", "true");

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

// create a message

Message msg = new MimeMessage(session);

// set the from and to address

InternetAddress addressFrom = new InternetAddress(from);

msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];

for (int i = 0; i < recipients.length; i++)

{

addressTo = new InternetAddress(recipients);

}

msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type

msg.setSubject(subject);

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

Transport.send(msg);

//Eval key

Eval dd=new Eval();

dd.keygenerator();

}

private class SMTPAuthenticator extends javax.mail.Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

String username = SMTP_AUTH_USER;

String password = SMTP_AUTH_PWD;

return new PasswordAuthentication(username, password);

}

}

public void keygenerator() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {

String inputString ="0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";

KeyGenerator keyGen = KeyGenerator.getInstance("HMACMD5");

SecretKey secretKey = keyGen.generateKey();

Mac mac = Mac.getInstance(secretKey.getAlgorithm());

mac.init(secretKey);

byte[] byteData = inputString.getBytes("UTF8");

byte[] macBytes = mac.doFinal(byteData);

String macAsString = new sun.misc.BASE64Encoder().encode(macBytes);

system.out.println("Athentication key is "+macAsString);

}

catch(Exception e)

{

System.err.println("error writing");

}

}

}

erikaa at 2007-7-7 22:15:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Yeah, okay, it looks like you have the right idea. Changing your keygenerator method to return the string instead of just printing it would be a start, then use the result of that method in the body of the message. But that's just basic beginner's Java, nothing specific to JavaMail at
DrClapa at 2007-7-7 22:15:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...