package org.karmaexchange.util;

import java.io.UnsupportedEncodingException;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.karmaexchange.dao.User;
import org.karmaexchange.dao.BaseDao.ResourceValidationError;
import org.karmaexchange.dao.User.RegisteredEmail;
import org.karmaexchange.resources.msg.ValidationErrorInfo.ValidationErrorType;



public class MailUtil {
	
	public static void sendMail(User fromUser, User toUser,String subject, String body)
	{
		sendMail(getPrimaryEmailForUser(fromUser),fromUser.getFullName(),getPrimaryEmailForUser(toUser),toUser.getFullName(),subject,body);
	}
	
	private static void sendMail(String fromEmail, String fromName, String toEmail, String toName, String subject, String body)
	{
		Session session = Session.getDefaultInstance(null);


		try {
		    Message msg = new MimeMessage(session);
		    msg.setFrom(new InternetAddress(fromEmail, fromName));
		    msg.addRecipient(Message.RecipientType.TO,
		     new InternetAddress(toEmail, toName));
		    msg.setSubject(subject);
		    msg.setText(body);
		    Transport.send(msg);

		} catch (AddressException e) {
		    // ...
		} catch (MessagingException e) {
		    // ...
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getPrimaryEmailForUser(User userObj){
		
		for (RegisteredEmail registeredEmail : userObj.getRegisteredEmails()) {
		      if (registeredEmail.isPrimary()) {
		    	  return registeredEmail.getEmail();
		      }
		}
		return null;
	}
	

}

@@ -391,170 +391,28 @@
 dao.
-BaseDao.ResourceValidationError;%0Aimport org.karmaexchange.dao.User.RegisteredEmail;%0Aimport org.karmaexchange.resources.msg.ValidationErrorInfo.ValidationErrorType
+User.RegisteredEmail
 ;%0A%0A%0A
@@ -436,18 +436,17 @@
 lUtil %7B%0A
-%09
 %0A
+
 %09public 
@@ -573,31 +573,35 @@
 er),
-fromUser.getFullName(),
+ getUserName(fromUser),%0A%09%09%09
 getP
@@ -631,37 +631,38 @@
 er),
-toUser.getFullName(
+ getUserName(toUser
 ),
+ 
 subject,
 body
@@ -657,16 +657,17 @@
 subject,
+ 
 body);%0A%09
@@ -668,17 +668,16 @@
 dy);%0A%09%7D%0A
-%09
 %0A%09privat
@@ -1355,17 +1355,125 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
+%0A%09private static String getUserName(User user) %7B%0A%09%09return user.getFirstName() + %22 %22 + user.getLastName();%0A%09%7D%0A
 %0A%09public
@@ -1525,18 +1525,16 @@
 erObj)%7B%0A
-%09%09
 %0A%09%09for (
@@ -1719,13 +1719,12 @@
 ull;%0A%09%7D%0A
-%09
 %0A%0A%7D%0A

Fix build failure.