File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes sending mails through java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "sending mails through java" Watch "sending mails through java" New topic

sending mails through java

vidhyasagar reddy
Ranch Hand

Joined: Jul 05, 2005
Posts: 40
while sending the mails it is showing the error at run time


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendMail
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException {
boolean debug = false;

//Set the host smtp address
Properties props = new Properties();
props.put("", "");

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
addressTo[i] = new InternetAddress(recipients[i]);
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type
msg.setContent(message, "text/plain");
System.out.println("before sending");

public static void main(String args[]) throws MessagingException

SendMail o = new SendMail();
String to[]={""};
String from="";
String msg="This is password";
String sub="Message from Here To Win";
System.out.println("sending over...");




At run time it is showing that unable to connect to the smtp port 25


some body told me that we have to install the james mail server for sending mails through java , i have downloaded that and while running the bin/run.bat

1.It is showing the error that Error while building apps/james/SAR-INF/config.xml

can any body tell em how to install the james server or tell me ur procedure for sending mails through java
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You've posted this in "Games Development"; I think you meant to post in "Other Java APIs." I will move this there for you.

[Jess in Action][AskingGoodQuestions]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I can't connect to on port 25 either. You should check whether that is in fact the correct mail server address.
James is an email server, but you're building an email client, so, no, you don't need James. javax.mail implements the standard email protocols - it can connect to any email server that also supports these standards (which means just about all of them). You shouldn't even have to know what kind of server you're connecting to, and it certainly doesn't need to be written in Java.
I agree. Here's the link:
subject: sending mails through java
jQuery in Action, 3rd edition