• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaMail step-by-step

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Transport object is normaly like this:


Rene
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no more questions.
If you want you can close the topic.
Thank you!
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic