This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: how to send email from java program
It's not a secret anymore!