Java = Get public IP & Send report ot email
CyberCodes :: COMMUNITY :: Computer and Internet Zone :: Programming :: JAVA
Page 1 of 1
Java = Get public IP & Send report ot email
mainclass
send mail class
- Code:
import java.io.ioexception;
import java.net.*;
import java.io.*;
public class dostuff {
public static void main(string args[]) throws ioexception {
try {
/* console output to file */
printstream out = new printstream(new fileoutputstream("output.txt"));
system.setout(out);
/* get public ip address */
java.net.url url = new java.net.url("http://www.whatismyip.org/"); //querry whatismyip.com
java.net.httpurlconnection conn = (httpurlconnection)url.openconnection();
java.io.inputstream instream = conn.getinputstream();
java.io.inputstreamreader isr = new java.io.inputstreamreader(instream);
java.io.bufferedreader br = new java.io.bufferedreader(isr);
system.out.print("public ip address: " + br.readline());
/* get private ip address */
inetaddress priv = inetaddress.getlocalhost();
system.out.println ("\nprivate ip address : " + priv );
/* copy file contents */
stringbuilder contents = new stringbuilder();
try {
bufferedreader input2 = new bufferedreader(new filereader("output.txt"));
try {
string line = null;
while (( line = input2.readline()) != null) {
contents.append(line);
contents.append(system.getproperty("line.separator"));
}
}
finally {
input2.close();
}
}
catch (ioexception ex) {
ex.printstacktrace();
}
/* convert stringbuilder to string */
string str = contents.tostring();
/* add email address to array and send report */
postmailc x = new postmailc();
string address [] = {"jsmith@mail.com"}; //enter your mail address here
x.postmail(address, "ip report", str, "remote@mail.com");
}
catch (exception e) {
e.printstacktrace();
}
}
}
send mail class
- Code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class postmailc {
public void postmail(string recipients[], string subject, string message , string from) throws messagingexception {
boolean debug = false;
/* configure smtp */
properties props = new properties();
props.put("mail.smtp.host", "smtp.yourisp.net"); //enter your isp's smtp server
/* create properties and get the default session */
session session = session.getdefaultinstance(props, null);
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[i] = new internetaddress(recipients[i]);
}
msg.setrecipients(message.recipienttype.to, addressto);
/* set subject and content type */
msg.setsubject(subject);
msg.setcontent(message, "text/plain");
transport.send(msg);
}
}
princeterror- Universal Moderator
- Posts : 272
Credits : 11465
Fame : 81
Join date : 2012-06-07
Age : 33
Similar topics
» Java Swing Calculator
» Java ternary Operator
» [TUT] Create a Frame in Java with 2 Labels
» JAVA - ArrayList Cloning Technique
» Report Forum Bugs Here
» Java ternary Operator
» [TUT] Create a Frame in Java with 2 Labels
» JAVA - ArrayList Cloning Technique
» Report Forum Bugs Here
CyberCodes :: COMMUNITY :: Computer and Internet Zone :: Programming :: JAVA
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum