A program that can send mails. This requires that you have activation.jar and mail.jar in you classpath.
Java Mailing
/*
to run this example u may require activation.jar and mail.jar in ur classpath
*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
import java.io.*;
public class SimpleSender
{
public static void main(String args[])
{
try
{
String smtpServer=args[0];
String to=args[1];
String from=args[2];
String body=args[3];
send(smtpServer, to, from,body);
}
catch (Exception ex)
{
System.out.println("Usage :\njava SimpleSender server to from body");
}
System.exit(0);
}
public static void send(String smtpServer, String to, String from, String body)
{
try
{
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
msg.setSubject("Test Mail thru java");
msg.setContent(body,"text/plain");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
monga monga 0 Newbie Poster
AhmedHan 0 Junior Poster in Training
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
brittoonline 0 Newbie Poster
Jocamps 14 Junior Poster
balagangadharm 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.