Hi, buddies,
On the new year 2008 eve, an idea just stroke into my mind that I would like to remote control my home PC if I am not at home. However, my family is not so familiar withe the operational skills so every time I have to teach them to run command "ipconfig", get the result and login to IM or email, then finally send to me.
It would be in a mess and very annoying if it keeps happening. That is the context of my program.
My program "Java email if internet detected" has functions in 3 ways below:
1. detect the internet is accessible or not
2. get the ip address of the host computer
3. login to Gmail and send an email about the IP and exact time of the message
4. make an executable Jar wrapping all the library jars
Java is the implementing language.
1.Detect internet accessible
In JDK1.5+, a function InetAddress.isReachable() can detect a specific host name is reachable by receiving IGMP echo ack message.
example:
public boolean isOnInternet(String pingAddr){
boolean isHooked=false;
if (pingAddr == "") pingAddr="www.163.com";//"web.mit.edu" is fine as well for port 7, but not google
try {
isHooked = InetAddress.getByName(pingAddr).isReachable(3000);
}catch(Exception exc){
exc.printStackTrace();
}
return isHooked;
}
echo ack message. I take "www.163.com" as a flag of internet accessibility. Please make sure "import java.net.*" when taking this function.
2. get the ip address of the host computer
Getting IP itself is not difficult because InetAddress.getLocalHost().getAddress() has already done it for us. However, the returned string has the raw IP address stored, which means we have to decode the raw IP address with the mask 0xff with the integer digital length and then turn it into integer , of coz , adding a coma "." to separate every pair.
example:
public String get_ip(){
byte[] ipAddr ={'0', '0', '0'};
String ipAddr_str="";
try {
InetAddress addr = InetAddress.getLocalHost();
ipAddr = addr.getAddress();
for (int index = 0; index <>
index++)
{
if (index > 0)
{
ipAddr_str += ".";
}
ipAddr_str += ((int)ipAddr[index])& 0xff;
}
} catch (UnknownHostException e) {
}
return ipAddr_str;
}
3.login to Gmail and send an email about the IP and exact time of the message
This part is a bit slight difficult and I decided to use the javax.mail package to handle.
Please pay attention the email protocol may be of SMTP, SMTPs and POP3, which has to be very specific. Different protocol leads to different server, such SMTP server usually is named "smtp.*.com".
Take Gmail as an example, since it is using SMTPs, in this case, it requires authentication for successfully logging into the SMTPs server.
example:
public void sendMail(String content) throws Exception{
String host = "smtp.gmail.com";
String from = "congliulyc@163.com"; //try to fake
String to = "congliulyc@gmail.com"; //"congcongcongliu@163.com";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", host);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
Session session = Session.getDefaultInstance(props,new MailAuthenticator() );
session.setDebug(false);
session.setProtocolForAddress("rfc822", "smtps");
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("On Internet Alert" );
message.setText("Auto mail by Jack, now is "+ sdf.format(cal.getTime()) + "\nAlert: " + content + " is on internet now.");//("This is the mail body");
System.out.println("JDebug: on "+ sdf.format(cal.getTime()) + "mail send request");
Transport.send(message);
}catch (Exception mex) {
System.out.println("send failed, exception: " + mex);
}
}
4. make an executable Jar wrapping all the library jars
I am using Eclipse as my developing IDE and when I am trying to distribute an executable jar, I found that "jar cmf mainClass.txt example.jar *.class" does not work because this project has involved some jar which is not JDK1.4 standard, such as Javax.mail. An Eclipse plug-in "Fat jar" in source-forge has been found out to compose this function, please refer to the web site http://fjep.sourceforge.net/
After exporting the whole project to a "fat jar", an executable one is stored on the project directory.
Executable Jar of V1.0 can be downloaded here.
Source code of V1.0 can be downloaded here.
Since the V2.0 of "Java email if internet detected" has been dispatched, please refer to here for more info.
Thanks very much.
1 条评论:
Happy 2008!
I have finished the coding on the 1st, Jan,2008 in Ottawa, Canada.
发表评论