# 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");
}
}
}