aspose file tools*
The moose likes Java in General and the fly likes JavaMail step-by-step Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JavaMail step-by-step" Watch "JavaMail step-by-step" New topic
Author

JavaMail step-by-step

fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
Hello! Can I be forwarded to a page that describes in detail
what steps to take to write a simple java mail-client?
I write one but it throws exceptions.
I need to know if I have to install smth additional.
Say I have a mail server running on another host with IP .......
I have an email account.
And all I want to do is to send a simple text(for beginning)into my
inbox folder.
If you could provide a simple example yourself with
protocol, url, username and password specified it would be great.
Thank you!
santosh kulkarni
Greenhorn

Joined: Jul 21, 2004
Posts: 18
Fedai,
You'll probably get more replies if you post what you tried and what exceptions you got.
java almanachas a simple program to send email


Impossible is not a fact its an opinion
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
You may try the example codes in the liks below:

Sending e-mail using JavaMail API

How to send an email with a file attachment
[ December 28, 2005: Message edited by: Casper Maxwell ]
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
Good day!
I give my thanks to you.
Everything worked fine.

I have another question.
The code whick worked is this


import javax.mail.*;
import javax.mail.internet.*;


public class simplemail {
public static void send(String smtpHost, int smtpPort,
String from, String to,
String subject, String content) {

try{

java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
Session session = Session.getDefaultInstance(props, null);
//Store store = session.getStore();
//Folder folder = store.getFolder("INBOX");
//System.out.println(folder.getMessage(1));

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);


Transport.send(msg);
}catch(Exception e){e.printStackTrace();}
}


public static void main(String[] args) throws Exception {

send("myhostname", 25, "Fedai@bankrespublika.az", "fedai@bankrespublika.az",
"Driving away", "I'm firing you");
}
}

When I uncomment the commented part it throws the exception

javax.mail.NoSuchProviderException: Invalid protocol: null
at javax.mail.Session.getProvider(Session.java:265)
at javax.mail.Session.getStore(Session.java:363)
at javax.mail.Session.getStore(Session.java:343)
at javax.mail.Session.getStore(Session.java:329)
at simplemail.send(simplemail.java:17)
at simplemail.main(simplemail.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)

Where in this code do I have to configure protocol?
Thank you!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

My Transport object is normaly like this:


Rene


Regards, Rene Larsen
Dropbox Invite
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
This isnt what I want!
I want to retrieve a specified message for the INBOX folder.
That's it.
How will this

Transport transport = session.getTransport("smtp");
transport.connect(hostname, username, password);
trabsport.send(message);

provide that?
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I used this.

Store store = session.getStore("smtp");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
System.out.println(message(1));

Although my INBOX folder contains a lot of messages it throws
the ArrayIndexOutOfBoundsException exception. (0>=0)

As I understand the getMessages method retrieves the messages from the INBOX folder and puts them into the message array. If my INBOX folder isnt empty then it must contain smth.
But it doesnt work.
What is the matter?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

OK, you are trying to do two thing at once - send and recive mail at the same time.

Try read this
Using the JavaMail API
Introducing the JavaMail API

Ren�
[ December 29, 2005: Message edited by: Rene Larsen ]
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
Thank you very much!
It worked!
And another question.
I am not familiar with JMS. I've just read couple of things on it.
If a create a java application on one machine and another one on another
machine. Can JMS be used here to make them communicate?
If yes forward me to a page describing how this can be done, please?
Thank you!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

The short answer to JMS is - YES

It depends on the use of the JMS queue � will the queue be located in a database e.g. Oracle, DB2� � then the answer is always YES � but will the queues be file based, then both applications must share the files location and a lot of other things � the answer is MAYBE.

This JMS Tutorial will give you the basic: Java Message Service Tutorial

Maybe you should read about RMI (Remote Method Invocation) also.

You will get a lot of information by doing a search on e.g. google.

Rene
[ December 30, 2005: Message edited by: Rene Larsen ]
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I have no more questions.
If you want you can close the topic.
Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaMail step-by-step
 
Similar Threads
need resource
uploading a photo
Supporting Different Screen Resolution Sizes
Struts-tags.tld
Help