Wednesday, December 5, 2007

Fake Mail

One can send mails using JAVA Mail API. It is fairly simple to use this library to send mails.

First download the JAVA Mail API, and add mail.jar to your classpath
Then download, JAF API, and add activation.jar to your classpath

Following is the simple code:

Java Class: TestMail.java
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.InetAddress;

class TestMail
{
public static void main(String args[])
{
try
{
//from address
String from = "fromaddress@somecompany.com";

//receipents of the mail
String to = "toaddress@someothercompany.com";

/*
*If there are multiple
*recepients, then email ids
*should be stored in a String
*with a space in between them.
*eg: String to = "id1@comp1.com id2@comp2.com id3@comp3.com"
*/

//subject of the mail
String subject = "Test Mail Subject";

//e-mail content
String text = "This is a test email";

/*SMTP host
*SMTP host should be correct
*for this program to work.
*Also, some SMTP servers
*are configured to allow
*messages to be sent from
*one or a few doamins
*/
String mailhost = "mail.somecompany.com";

//mailer
String mailer = "testmailer";

Properties props = System.getProperties();

props.put("mail.smtp.host", mailhost);

Session session = Session.getInstance(props, null);

//construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());

//send the mail
Transport.send(msg);

System.out.println("\nMail was sent successfully.");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

WARNING:
When full headers are viewed by the recepeint for a mail sent by this program, it mite have an indication that there is something fishy about how the mail was sent !!


Happy Spamming !!!

No comments: