Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to send email from java program

 
Ramaswamy Narayanan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic