INTERNET EXPLORER? Ti consiglio di navigare questo sito con Mozilla Firefox oppure con Google Chrome.

Si può spedire o ricevere email da un'Applet Java?

Autore: it.comp.java | Tell a Friend

Corso Java
Impara a programmare col linguaggio OOP della Sun.

Corso Javascript e DHTML
Impara a creare controlli ed animazioni!

Corso Web Design
Disegno di siti Web con HTML, CSS e Dynamic HTML.

E' possibile. Ti allego un esempio che ho avuto da un tecnico Sun Utilizzare previa installazione di JavaMail

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

/**
    * usage: sendmessage to from smtphost multipart
    *
    * Send a simple text/plain message to the "to"
    * address, from the "from" address, using the
    * smtphost as the machine with the smtp server
    * running.
    *
    * if multipart is "true" send a multipart message
    * else if multipart is "false" send a text/plain
    * message.
*/ 

public class sendmessage
{
    public static void main(String[] args)
    {
        if (args.length != 4)
        {
            System.out.println("usage: sendmessage <to> <from> <smtphost> <true|false>");
            System.exit(1);
        } 
        boolean debug = false;
        // change to get more information
        String msgText = "A body.
the second line.";
        String msgText2 = "Another body.
more lines";
        boolean sendmultipart = Boolean.valueOf(args[3]).booleanValue();
        // set the host
        Properties props = new Properties();
        props.put("mail.smtp.host", args[2]);
        // create some properties and get the default Session 
        Session session = Session.getDefaultInstance(props, null); 
        session.setDebug(debug); 
        try // create a message
        {
            Message msg = new MimeMessage(session);
            // set the from
            InternetAddress from = new InternetAddress(args[1]);
            msg.setFrom(from);
            InternetAddress[] address = {new InternetAddress(args[0])};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("JavaMail APIs Test");
            if (!sendmultipart) // send a plain text message
            {
                msg.setContent(msgText, "text/plain");
            }
            else
            {
                // send a multipart message
                // create and fill the first message part
                MimeBodyPart mbp1 = new MimeBodyPart();
                mbp1.setContent(msgText, "text/plain");
                // create and fill the second message part
                MimeBodyPart mbp2 = new MimeBodyPart();
                mbp2.setContent(msgText2, "text/plain");
                // create the Multipart and its parts to it
                Multipart mp = new MimeMultipart();
                mp.addBodyPart(mbp1);
                mp.addBodyPart(mbp2);
                // add the Multipart to the message
                msg.setContent(mp);
            }
            Transport.send(msg);
        }
        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }
    }
}
by sergiolc[AT]iol.it

PS: RTFM. Un'Applet *non puo'* aprire una socket verso un host diverso da quello da dove e' stata prelevata, a meno di non richiederne esplicitamente i diritti, accompagnando la richiesta con una firma digitale (e solo in Java 1.1, con un browser che supporti le signed Applets - leggi Communicator). Indi, o piazzi la tua Applet su [lo stesso server su cui gira il server di mail -cs] - oppure *non va*.

IN EVIDENZA
HOT LINKS