my question is that can we send e-mail through code written in java? i know that it can be made in php but don't knoe about java...if answer is yes, then can u give me hint that how to proceed means what to use(API, servlets or jsp or link from which i can take hint)? if no, then why is it so ? thanks in advance. any help will be appreciated.

Recommended Answers

All 23 Replies

yes, that is possible.
just take a look at this thread.

The Desktop API (Java 1.6 and later) includes sending emails via your system's default email application
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
It's a LOT simpler than the old javamail api.

i have copied a code from somewhere and it is saying that javax.mail.internet.*; doesnot exist. how is it possible ? i never get any problem like any package is not there in my JDK.

some api's you need to download yourself and import them in your application.

some api's you need to download yourself and import them in your application.

sir! i have downloaded javax.mail.jar file from net. now, how to include it in my packages. means how compiler will now that where is javax.mail package ?

I assume you're using an IDE.
you can add the .jar file to the resources of that project.

I assume you're using an IDE.
you can add the .jar file to the resources of that project.

no no i am not using IDE. also, tell me from where can i learn how to send e-mail from starting as i don't have any idea how to send e-mail.
link james cherril has given doesn't contain much about how to send email. help!!

the link I provided you is an earlier thread where you find all the information you need to send an email using a gmail account.
if you want to use other addresses, you 'll have to change some of the information

take a look at this tutorial about using classes in other jar files.

link james cherril has given doesn't contain much about how to send email. help!!

Look again. About 2/3 down that page, under the heading "Sending Email" is a complete example with explanations, but, in short:
You create a new URI with the mail messsage, eg
mailto:duke@sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!
and call Desktop's mail method with the URI as a parameter to send it.
How easy is that?

Look again. About 2/3 down that page, under the heading "Sending Email" is a complete example with explanations, but, in short:
You create a new URI with the mail messsage, eg
mailto:duke@sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!
and call Desktop's mail method with the URI as a parameter to send it.
How easy is that?

i have got an idea, but how to create this uri ? means i am not exactly clear. i hope u r understanding me. how to create this URI like mailto:.... ?

i have got an idea, but how to create this uri ? means i am not exactly clear. i hope u r understanding me. how to create this URI like mailto:.... ?

read the article. it clearly states:


You can use more than just a single email recipient when creating this URI. The mailto scheme supports CC, BCC, SUBJECT, and BODY fields as well. For example, the following text could be used to create a mailto URI:

mailto:duke@sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!

read the article. it clearly states:

ya ya i read that whole. but , when i take gettext() means duke@sun.com then how to create this uri which contain subject and all? what will the sender's id in this case?

Did you download the source code and check(view/edit/test) the contents to see how it works?

Did you download the source code and check(view/edit/test) the contents to see how it works?

which source code u are talking about ? i have wrote this code :

import java.awt.Desktop;
import java.net.URL;
import java.net.URI;
class mail2
{
	public static void main(String args[])
	{
		Desktop des=null;

		String mailTo="gourav.singhal@gmail.com";
		try
		{

		URI  uriMailTo = new URI("mailto", mailTo, null);

		des.mail(uriMailTo);
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

and this is throwing (when i run it) nullpointerException..what is the problem ???

Look again. About 2/3 down that page, under the heading "Sending Email" is a complete example with explanations, but, in short:
You create a new URI with the mail messsage, eg
mailto:duke@sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!
and call Desktop's mail method with the URI as a parameter to send it.
How easy is that?

i have attached a pic which shows the screen which loads when i click launch mail...it is different from the one in example of james cherril link. also, there is not any send option in this window.

i have attached a pic which shows the screen which loads when i click launch mail...it is different from the one in example of james cherril link. also, there is not any send option in this window.

to quote the article:

the host's default email client,

so what email client is opened, depends on your machine.

you say that a nullpointerexception is thrown. where is it thrown? can you provide us with the entire error message?

to quote the article:


so what email client is opened, depends on your machine.

you say that a nullpointerexception is thrown. where is it thrown? can you provide us with the entire error message?

ya! it is saying that nullpointer exception is thrown in main function

yeah ...
it also contains information on which line, method and variable contains null.
most of the times, it's very clear where the problem is, but we'll need to know a bit more than just 'nullpointerexception is thrown'

I just read that bit of code.

your instance of Des (des) is null, (not initialized) and you're calling a method on that. there's your problem

yeah ...
it also contains information on which line, method and variable contains null.
most of the times, it's very clear where the problem is, but we'll need to know a bit more than just 'nullpointerexception is thrown'

I just read that bit of code.

your instance of Des (des) is null, (not initialized) and you're calling a method on that. there's your problem

so how to initialize it please tell ?

Since I don't know the Desktop class .. I don't know
check the api's to see what constructors there are, or if those are static methods.

A quick search in that article told me this:

Desktop desktop = null;
 // Before more Desktop API is used, first check 
 // whether the API is supported by this particular 
 // virtual machine (VM) on this particular host.
 if (Desktop.isDesktopSupported()) {
     desktop = Desktop.getDesktop();
 }

how much experience do you have in coding? solving that nullpointerissue was very basic, we just didn't read all the code yet, expecting you to:
a. provide us with the entire error message
b. have read the entire article, or at least the parts you could have guessed were essential to get the job done.

A quick search in that article told me this:

Desktop desktop = null;
 // Before more Desktop API is used, first check 
 // whether the API is supported by this particular 
 // virtual machine (VM) on this particular host.
 if (Desktop.isDesktopSupported()) {
     desktop = Desktop.getDesktop();
 }

how much experience do you have in coding? solving that nullpointerissue was very basic, we just didn't read all the code yet, expecting you to:
a. provide us with the entire error message
b. have read the entire article, or at least the parts you could have guessed were essential to get the job done.

i am just 18 years old and don't have too much experince

well ...
I admire your will to learn how to develop serious applications, but IMHO it would help you a lot if you would slow down a bit, and learn the basics of the language.

in an earlier thread you said you wanted to take a certification exam. well, the questions on exams like that are exactly like this thread: you get a piece of code, where there might (not) be a mistake like that, just not this obvious, and you are asked what this code will do.

you won't be able to test it, to run the code or to ask for anyone's help.
there's a lot of material out there that can help you increase your knowledge, but the best way is like everything in life: one step at a time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.