When two individuals know nothing about each other, there is no way they can have a relationship, unless at least one of them knows the other sparking a two way knowledge and awareness that will finally end in a relationship.
As for having a relationship with Imam Mahdi (as), he is one end of the relationship that knows us very well and has affection for us. What is left for us, the other end, is to know our imam well and become worthy enough to see him, as has been the case with many righteous and pious individuals.
A relationship with Imam Mahdi (aj) can be in one of two ways; an inner relationship or an in-person one. Although both are possible, yet the latter calls for different "prerequisites". Having an inner connection with the imam and living up to this inner relationship can be considered one of the prerequisites for meeting him in person.
In order to reach an appropriate answer to your question, the following points must be considered:
1- When we say relationship a two way link is meant. In order to have a relationship, two individuals are needed who are willing to have a link between them, therefore, only having one individual isn’t enough for a relationship to take place.
Also, two people who aren’t in any way acquainted can't have a relationship either, but if a person knows another and likes him [while the other doesn’t know him], there are chances it will end with the two having a relationship in the future.
As for establishing a relationship with Imam Mahdi (aj), he is one end of the relationship who is aware of and knows the other end very well, to the extent that hadiths tell us that Imam Mahdi loves all of the Shia and his followers, and that he is aware of everything they do. It has been narrated that Imam Mahdi (aj) addressed Sheikh Mufid, one of the Shia's great scholars, saying: "انا غیر مهملین لمراعاتکم و لا ناسین لذکرکم", which means "We aren’t indifferent about you [the Shia] and never forget you." This is the highest level of love that can be achieved by anyone. Do any of us have any friends who love us to the extent that they are thinking about us and feel responsible about us at all times? So one end of this relationship is set; Imam Mahdi (aj) is always anticipating his followers, what is left is for us to do our part so that a true relationship can be established.
Clearly, one who doesn’t know his imam, or doesn’t know him the way he is supposed to, can't have a relationship with him. The reason for such being that awareness of another's virtues, morality and spiritual or sometimes even physical beauties is what causes one to like and have the tendency to set up a relationship with him, given that the two reach the conclusion that the other is worthy of such a relationship. As was said, the imam loves us all, but do we also love him? Or do we just claim that we do? Have we even found out that the imam (aj) loves us all so that we can love him?
2- As was mentioned, relationships are contingent upon acquaintance and awareness, followed by liking and love. Now if a person strengthens and tightens his/her relationship with the imam in his/her heart, reaching a point in which he/she gains the worthiness to see him, he/she absolutely will be able to see and meet him. There are many stories of great individuals and scholars who had worked on and built themselves spiritually, eventually reaching the very fine destination of meeting the imam (aj); some recognizing him, while others realizing who they had met later. The stories of Haj Ali Baghdadi and Haj Seyyed Ahmad Rashti, which can be found in the Mafatihul-Jinan, are two of these many stories.
Clarification: There are two types of relationships one can have with Imam Mahdi (aj); a) In person b) In the heart. The question is, are both of these relationships possible? In order to answer this question, we will analyze each of the two separately:
a) Meeting the imam in person: Meaning that the two meet each other physically; this can be in one of two ways:
i) To see the imam but not recognize him
According to hadiths, this is possible for all and takes place as well. Saduq, a great Shia scholar narrates from one of the imam's "specific" representatives that he said: "Imam Mahdi (aj) participates in the Hajj ceremony every year; he sees the people and knows all of them, yet the people see him but don’t recognize him." So not only isn’t seeing the imam possible, but it is also takes place, nevertheless, such a seeing can't be called having a relationship with him, and this isn’t what your question is about.
ii) To see the imam and recognize him:
Is it possible for one to see the imam and recognize him? Yes it is possible, and the real question is, if one is righteous and pious enough to be worthy of seeing the imam, why wouldn’t the imam want to see him?! There is no reason for Imam Mahdi (aj) not seeing anyone until the zuhur [his emergence]. He hasn’t sworn to not see anyone till then. There is one exception though, and that is that there are sometimes other reasons that make it better for the imam not to meet a certain person who is worth [for instance, sometimes it is for the person's own good not to the see the imam].
There are many stories of great scholars and individuals that tell us of them meeting the imam. Some of them recognized the imam while others realized who they had met after they/he had left.
The point that needs to be made here is that meeting the imam doesn’t always have to be in person, although meeting him in person is a very extraordinary and sweet experience. This type of meeting doesn’t take place for everyone and seldom does the imam meet someone in such a way. What is important is for all of us to at least have an inner connection with him.
2- Having a relationship with him in the heart:
This means for one to be in touch with the imam through the soul and heart, regardless of whether he speaks and prays for the imam orally or in the heart, without ever experiencing his physical presence.
Is this type of relationship possible? Of course it is; no matter where we are and how we speak with them, the imams hear our voices and are aware of the situations we are in. When entering the shrines of the imams, we say: "اشهد أنک تسمع کلامی و تشهد مقامی", which means "I bear witness that you hear my words and are aware of my standing". There are numerous hadiths which tell us that the Prophet (pbuh) and imams (as) are aware of all of us and the situations we are in. In the story of Uways Qarani [the person who left his old mother to travel to Medinah to see the Prophet (pbuh) with her permission, given that he would return by the end of the night, and after reaching Medinah, realized that the prophet (pbuh) was out of town, and would return at a time that if Uways was to await him, wouldn’t reach home in time, thus breaking his promise to his mother, so he left Medinah without seeing the prophet (pbuh), despite his deep love to see him], we read that he loved the prophet (pbuh) very much and although he longed to see him, he never succeeded, yet the prophet (pbuh) knew of him and his extreme affection for the prophet (pbuh). In a hadith, the prophet (pbuh) says: "The scent of heaven blows from Qaran's direction, O Uways, how I long to see you! [The prophet (pbuh) was very pleased of the fact that Uways had returned to his home without succeeding in seeing him, just to fulfill his promise to his mother and for the sake of Allah (swt)] Send him my salams if you ever see him." He goes on to say: "He will never see me, and after me, will be martyred while fighting on Ali's (as) side in the battle of Siffin."
So the prophet (pbuh) and imams (as) are aware of us and what we have in our hearts, regardless of whether we verbalize it or not. Thus, we always have the chance to get in touch and speak with them anytime we want.
Here, we would like to draw your attention to some advice and tips given by The Grand Ayatullah Bahjat [who is known for his high spirituality] for those who want to see and meet the imam:
In response to a question from those who want to meet the imam in person, he says: "Recite salawats frequently, [Allahumma Salli Ala Muhammadin wa Ali Muhammad], adding the wa ajjil farajahum [which means "and hasten their aid [to us]" (aid here refers to the return of Imam Mahdi)] to the end, and offer them [their blessings and rewards] to the imam (aj), go to the Jamkaran mosque on a regular basis and pray the the special prayers there."
In another place, concerning the ways one can use to establish a relationship with the imam, he says: "Establishing a relationship with Allah (swt) is contingent upon obeying Him and Imam Zaman (aj), and in order to know that we are doing so, we need to act according to what Islam has asked us to do, which calls for us to refer to a risalah [the books in which Islamic marja's record their verdicts on Islamic law] that can be followed [and apply all of the rules therein to our everyday lives]."
In response to another question about how we can strengthen our relationship with the imams, especially Imam Mahdi (aj), he says: "Obeying Allah (swt) after understanding who He is and getting familiar with him, brings love for Him, and love for those Allah (swt) likes, who are the prophets (pbut) and their successors, in which the most beloved of all of them are the prophet of Islam (pbuh) and his progeny (as), and the closest of them to us is Imam Mahdi (aj)."
Other ways for establishing a relationship with the imam have also been mentioned elsewhere:
2-1 Reciting the dua of اللهم کن لولیک ... for his health and well-being.
2-2 Reciting Dua Ahd which Imam Sadiq (as) has said that whosoever recites this dua during the sabah [between dawn and sunrise] will be one of the companions of Imam Mahdi (aj).
3-2 Reciting Ziyarah Ale-Yasin, which Imam Mahdi (aj) has said: "Whenever you want to focus on us, recite this ziyarah." It can be found in Mafatihul-Jinan.
4-2 Reciting Ziyarah Jame'ah Kabirah which can also be found in Mafatihul-Jinan.
Therefore, it is possible to establish a relationship with Imam Zaman (aj). It just depends on how much we want and strive for it. It is both possible to have an inner connection with him, or, in the case of very hard struggle and effort by being pious and staying away from sins, even see and meet him. One can even reach a level that the imam himself will come to him! Of course, we should all keep in mind that if one does all of the things mentioned above and still doesn’t succeed in seeing the imam (aj), he/she shouldn’t lose hope, but should try harder and harder and be more careful of his/her actions. Finally, there are always chances of a person being totally worthy of seeing the imam, yet other circumstances don’t allow such a thing to happen, such as seeing him not being to the benefit of the individual, and we should be aware of this fact and not lose hope, but continue our way, because we have been brought to this world to serve and obey Allah (swt), regardless of whether we see our imam (as) or not, and that is what being a true servant of Allah (swt) is; to serve Him sincerely, without expecting anything in return [although he never leaves our good actions without any reward].
3- A question might come up here which is: "If it is possible to see the imam (aj), then how come there are hadiths that tell us to deny the claims of those who say they have seen the imam?
The answer to this question is that there are hadiths that tell us whoever claims to have seen the imam before the emergence of the Sufyani and "the cry" is a liar. Regarding these hadiths, our scholars have said: "What the imam means by "seeing" [when he says those who claim to have seen me], is for one to claim to have seen him and say that he is the imam's "specific" representative, like the four representatives he had during his minor occultation. In any case, none of these hadiths reject the possibility of seeing the imam, all they say is to deny the claims of those who say they have seen him. When we read about the lives of some of the great individuals who had seen the imam (aj), we see that none of them neither announced nor took advantage of what had taken place for them; in many cases, they had concealed it until their deaths and after that others had found out.
Without a doubt, being able to see the imam (aj) takes a lot of hard work and effort.
4- The final point that needs to be made is the importance of maintaining a relationship with him for his followers, because as we all know, when one has a relationship with those higher than him in spirituality and morality, he strives to become like them or at least more like them than before. It has been proven in psychology that every person is in need of a "role model". For instance, when a youth establishes an inner relationship with Imam Mahdi (aj), it has an impact on his life and he tries to do things that will please his master; this will eventually end in his spiritual progression. Thus we can say that a relationship with the imam can play the role of an "engine" in one's life; pushing towards good deeds and morality. When a youth sees all of the dignity, grace and virtues of his imam, he will try to establish a relationship with him, and of course, such a matter doesn’t necessarily have to be through physically getting in contact with the imam; all people can have a relationship with him by reading about him, his characteristics, attributes, virtues and way of life, and following him in all of them and becoming more like him. Certainly, the imam's vast existence is to the extent that whenever one gets in touch with him through the heart, he also pays attention to the individual, as corroborated by different ziyarahs that say: "I bear witness that you hear my words and see my standing". Therefore, it isn’t our duty to see the imam in person, although it would be a great blessing for such a thing to happen, nevertheless, if one doesn’t ever see the imam, it doesn’t mean that the imam is indifferent and doesn’t care about him/her.
 Sheikh Saduq, Ihtijaj, vol. 2, pg. 497.
 Biharul-Anwar, vol. 52, chapter 18 (Al-Ghibah); Muntahal-Amal, vol. 2, chapter 14, title 5.
 ان صاحب هذا الامر یحضر الموسم کل سنة یری الناس و یعرفهم و یرونه و لا یعرفونه Man La Yahdhuruhul-Faqih, vol. 2, pg. 520, hadith 3115).
 Biharul-Anwar, vol. 97, pg 375, chapter 5, hadith 9.
 Biharul-Anwar, vol. 42, pg. 155, chapter 124, hadith 22.
 Seyyid Mahdi Sa'i, Beh Sooye Mahboub, pg. 59.
 Ibid, pg. 61.
 Kafi, vol. 4, pg. 162, chapter الدعا فی العشر الاواخر (supplications for the final ten days), hadith 4.
 Biharul-Anwar, vol. 53, pg. 95, chapter 29, hadith 11.
 Biharul-Anwar, vol. 99, pg. 81, chapter 7, hadith 1.
 Man La Yahdhuruhul-Faqih, vol. 2, pg. 609, hadith 3213.
 These are two of the signs ofZuhur.
 Biharul-Anwar, vol. 151, chapter 23, hadith 1.
 Allamah Majlisi quotes this interpretation of this hadith under it in Biharul-Anwar from the book Ikmaluddin.
 Biharul-Anwar, vol. 97, pg. 375, chapter 5, hadith 9 "أشهد أنک تسمع کلامی و تشهد مقامی".