Meaningless Drivel is fun!*
The moose likes Other JSE/JEE APIs and the fly likes how to send email from java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "how to send email from java program" Watch "how to send email from java program" New topic

how to send email from java program

Ramaswamy Narayanan

Joined: Jan 19, 2005
Posts: 24
hai friends

i am doing a project. in that, i have to send email on clicking a button.
is it possible from java.

actually i got some help from a site.
it is we have to use javamail api.
but in that program i have to give a isp's mail server.
what it means?
if anybody knows, please help me.
thank you
Ganesh Phapale

Joined: Dec 14, 2004
Posts: 21
what exactly you want?
code in java / JSP?

if you hage got the code you have to specify ur SMTP server IP in it so as to send the mail.
Properties props = System.getProperties();

// Puts the SMTP server name to properties object
props.put("", smtp);

here is the link to java mail APIs to know more abt sending mail in java

here is a link to send mail with attachment:
Ramaswamy Narayanan

Joined: Jan 19, 2005
Posts: 24
Actually my code is
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

// Send a simple, single part, text/plain e-mail
public class TestEmail {

public static void main(String[] args) {

String to = "";
String from = "";
String host = "";

// Create properties, get Session
Properties props = new Properties();

// If using static Transport.send(),
// need to specify which host to send it to
props.put("", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);

try {
// Instantiatee a message
Message msg = new MimeMessage(session);

//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());

// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");

//Send the message
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
}//End of class

here what is the isp's server(what is host name)?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

but in that program i have to give a isp's mail server.
what it means?

It means you have to give Java Mail the address of the SMTP server it can use to send emails. So you need to get in touch with whoever administers the SMTP server you want to use and find out its DNS name or IP address.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: how to send email from java program
Similar Threads
javax/mail/Message error
easiest way to send an email from a java program?
replying an email
long wait on paper certificate-what to do now?
Need to Invoke a Java Program from a trigger