GeeCON Prague 2014*
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


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
Author

how to send email from java program

Ramaswamy Narayanan
Greenhorn

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
Greenhorn

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("mail.smtp.host", smtp);
===========================================================

here is the link to java mail APIs to know more abt sending mail in java
http://java.sun.com/products/javamail/javadocs/index.html

here is a link to send mail with attachment:
http://www.jguru.com/faq/view.jsp?EID=30251
Ramaswamy Narayanan
Greenhorn

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) {

// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "abcd@yahoo.co.in";
String from = "zyxw@yahoo.co.in";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "mail.yahoo.com";

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

// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", 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
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
}//End of class

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

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
 
GeeCON Prague 2014
 
subject: how to send email from java program