This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Need Help in creating a Java Mailer 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 "Need Help in creating a Java Mailer" Watch "Need Help in creating a Java Mailer" New topic

Need Help in creating a Java Mailer

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi All,

I am trying to send a mail from my Java Program.
The Code i used is:
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendApp {
public static void send(String smtpHost, int smtpPort,
String from, String to,
String subject, String content)
throws AddressException, MessagingException {
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
Session session = Session.getDefaultInstance(props, null);

// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Send the message

public static void main(String[] args) throws Exception {
// Send a test message
send("", 25, "", "",
"re: dinner", "How about at 7?");

I am getting the exception "UnknownHostException".

Can anybody tell me a valid SMTP host so that i can fix this problem.

Thanks in Advance!!!
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41019
Can you ping that host? (I can.)

I'm certain that you also need to provide username and password in your code. No mail server lets just anybody connect and send mails through it these days.

Ping & DNS - my free Android networking tools app
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134

Could you please explain how to provide username and password? Is there any API for that?

Abhijith Prabhakar
Ranch Hand

Joined: Dec 29, 2006
Posts: 56
You have to use authenticator for the same. I have given the complete method used by me. Hope this helps

* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.*/

private class SMTPAuthenticator extends javax.mail.Authenticator

public PasswordAuthentication getPasswordAuthentication()
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);

/* To use this program, change values for the following three constants,

SMTP_HOST_NAME -- Has your SMTP Host Name
SMTP_AUTH_USER -- Has your SMTP Authentication UserName
SMTP_AUTH_PWD -- Has your SMTP Authentication Password

Next change values for fields

emailMsgTxt -- Message Text for the Email
emailSubjectTxt -- Subject for email
emailFromAddress -- Email Address whose name will appears as "from" address

Next change value for "emailList".
This String array has List of all Email Addresses to Email Email needs to be sent to.


private static final String SMTP_HOST_NAME = "smtphostname";
private static final String SMTP_AUTH_USER = "test123";
private static final String SMTP_AUTH_PWD = "password";
private static final String emailMsgTxt = "Your request for resources has been placed. Delivery operations team will work on this & get back to you.\n\nRegards\nDOM team";
private static final String emailSubjectTxt = "Your request has been placed";
private static final String emailFromAddress = "";

public void SendMailUsingAuthentication(Context context){
Session session = null;
boolean debug = false;
String[] emailList = {context.getEmp_Mail_ID()};
Properties props = new Properties();
props.put("", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");

javax.mail.Authenticator auth = new SMTPAuthenticator();
session = Session.getInstance(props, auth);
catch(Exception expSession){
System.out.println("***********************exceptoin in try first****************");

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

// set the From, TO and CC address
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
InternetAddress[] addressTo = new InternetAddress[emailList.length];
for (int i = 0; i < emailList.length; i++)
addressTo[i] = new InternetAddress(emailList[i]);

msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setContent(emailMsgTxt, "text/plain");

System.out.println("***********************B4 send****************");
System.out.println("********************** Mail Sent****************");
catch(Exception e){
}catch(Exception ex) {
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Abijith,

Thanks for your suggestions. I am getting the UnknownHostException while trying to use the So if it is possible please share some mail host names.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41019
So if it is possible please share some mail host names.

What's wrong with using the mail server of your ISP? These days, nobody is going to let you use their mail server without you being properly identified.
wood burning stoves
subject: Need Help in creating a Java Mailer
Similar Threads
japanese character as junk
simple javamail example getting error
can not run simple javamail sample
Give me solution to send mail
Send Mail Error