Autore Topic: Problema nell'invio delle email  (Letto 654 volte)

Offline Pilatus

  • Nuovo arrivato
  • *
  • Post: 1
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy S2 Plus
  • Sistema operativo:
    Windows 7
Problema nell'invio delle email
« il: 17 Maggio 2015, 20:38:56 CEST »
0
Ciao a tutti,

vorrei sviluppare un'app Android che consenta di inviare delle email. Come prima versione, ho provato a creare un'app che invii un messaggio standard al mio account "libero", semplicemente premendo un pulsante.

Questa versione, però, sembra non funzionare né in simulazione né sul mio dispositivo Android.

Vi invio i sorgenti java ed il logcat ottenuto dopo avere provato ad inviare una mail.

Spero possiate aiutarmi.

Grazie.

Codice (Java): [Seleziona]
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                final Button send = (Button) this.findViewById(R.id.send);
                send.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                try {
                                        EMailSender sender = new EMailSender();
                                        sender.sendMail("This is Subject",
                                                        "This is Body",
                                                        "xxxx@libero.it",
                                                        "xxxx@libero.it");

                                } catch (Exception e) {
                                        Log.e("Error", e.getMessage(), e);  
                                }
                        }
                });

        }
}

Codice (Java): [Seleziona]
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class EMailSender extends javax.mail.Authenticator {
        private String mailhost = "smtp.libero.it";
        private String user = "xxxx@libero.it";
        private String password = "xxxx";
        private Session session;

        public EMailSender() {
                Properties props = new Properties();
                props.put("mail.transport.protocol", "smtp");
                props.put("mail.user", user);
                props.put("mail.host", mailhost);
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.port", "465");
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class",
                                "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.socketFactory.fallback", "false");
                props.setProperty("mail.smtp.quitwait", "false");

                session = Session.getDefaultInstance(props, this);
        }

        protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
        }

        public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
                try {
                        final MimeMessage message = new MimeMessage(session);
                        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
                        message.setSender(new InternetAddress(sender));
                        message.setSubject(subject);
                        message.setDataHandler(handler);
                        if (recipients.indexOf(',') > 0)
                                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
                        else  
                                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                                        try {
                                                Transport.send(message);
                                        } catch (MessagingException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                                }
                        }).start();
                } catch(Exception e){
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public class ByteArrayDataSource implements DataSource {
                private byte[] data;
                private String type;

                public ByteArrayDataSource(byte[] data, String type) {
                        super();
                        this.data = data;
                        this.type = type;
                }

                public ByteArrayDataSource(byte[] data) {
                        super();
                        this.data = data;
                }

                public void setType(String type) {
                        this.type = type;
                }

                public String getContentType() {
                        if (type == null)
                                return "application/octet-stream";
                        else  
                                return type;
                }

                public InputStream getInputStream() throws IOException {
                        return new ByteArrayInputStream(data);
                }

                public String getName() {
                        return "ByteArrayDataSource";
                }

                public OutputStream getOutputStream() throws IOException {
                        throw new IOException("Not Supported");
                }
        }
}

Codice (Text): [Seleziona]
05-17 17:06:36.459: E/Trace(637): error opening trace file: No such file or directory (2)
05-17 17:06:37.069: D/gralloc_goldfish(637): Emulator without GPU emulation detected.
05-17 17:15:39.099: I/System.out(637): DEBUG: JavaMail version 1.4.1
05-17 17:15:39.099: I/System.out(637): DEBUG: not loading file: /system/lib/javamail.providers
05-17 17:15:39.160: I/System.out(637): DEBUG: java.io.FileNotFoundException: /system/lib/javamail.providers: open failed: ENOENT (No such file or directory)
05-17 17:15:39.160: I/System.out(637): DEBUG: !anyLoaded
05-17 17:15:39.179: I/System.out(637): DEBUG: not loading resource: /META-INF/javamail.providers
05-17 17:15:39.179: I/System.out(637): DEBUG: not loading resource: /META-INF/javamail.default.providers
05-17 17:15:39.179: I/System.out(637): DEBUG: failed to load any providers, using defaults
05-17 17:15:39.189: I/System.out(637): DEBUG: Tables of loaded providers
05-17 17:15:39.210: I/System.out(637): DEBUG: Providers Listed By Class Name: {com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc.,1.4.1], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc.,1.4.1], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc.,1.4.1], com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc.,1.4.1], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,1.4.1]}
05-17 17:15:39.258: I/System.out(637): DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,1.4.1], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc.,1.4.1], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc.,1.4.1], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc.,1.4.1], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc.,1.4.1]}
05-17 17:15:39.259: I/System.out(637): DEBUG: not loading resource: /META-INF/javamail.default.address.map
05-17 17:15:39.259: I/System.out(637): DEBUG: !anyLoaded
05-17 17:15:39.259: I/System.out(637): DEBUG: not loading resource: /META-INF/javamail.address.map
05-17 17:15:39.269: I/System.out(637): DEBUG: not loading file: /system/lib/javamail.address.map
05-17 17:15:39.269: I/System.out(637): DEBUG: java.io.FileNotFoundException: /system/lib/javamail.address.map: open failed: ENOENT (No such file or directory)
05-17 17:15:39.269: I/System.out(637): DEBUG: failed to load address map, using defaults
05-17 17:15:39.611: I/System.out(637): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
05-17 17:15:39.749: I/System.out(637): DEBUG SMTP: useEhlo true, useAuth true
05-17 17:15:39.749: I/System.out(637): DEBUG SMTP: useEhlo true, useAuth true
05-17 17:15:39.749: I/System.out(637): DEBUG SMTP: trying to connect to host "smtp.libero.it", port 465, isSSL false
05-17 17:15:40.878: D/dalvikvm(637): JIT unchain all for threadid=1
05-17 17:15:41.388: D/dalvikvm(637): JIT unchain all for threadid=1
05-17 17:15:41.508: D/dalvikvm(637): GC_CONCURRENT freed 260K, 4% free 8179K/8519K, paused 351ms+347ms, total 884ms
05-17 17:15:42.108: I/System.out(637): 220 smtp-34.iol.local bizsmtp ESMTP server ready

05-17 17:15:42.108: I/System.out(637): DEBUG SMTP: connected to host "smtp.libero.it", port: 465
05-17 17:15:42.138: I/System.out(637): EHLO localhost

05-17 17:15:42.228: I/System.out(637): 250-smtp-34.iol.local hello [5.170.136.45], pleased to meet you

05-17 17:15:42.238: I/System.out(637): 250-HELP

05-17 17:15:42.238: I/System.out(637): 250-AUTH LOGIN PLAIN CRAM-MD5 DIGEST-MD5

05-17 17:15:42.248: I/System.out(637): 250-SIZE 50000000

05-17 17:15:42.248: I/System.out(637): 250-8BITMIME

05-17 17:15:42.248: I/System.out(637): 250-STARTTLS

05-17 17:15:42.248: I/System.out(637): 250 OK

05-17 17:15:42.258: I/System.out(637): DEBUG SMTP: Found extension "HELP", arg ""
05-17 17:15:42.367: I/System.out(637): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5 DIGEST-MD5"
05-17 17:15:42.368: I/System.out(637): DEBUG SMTP: Found extension "SIZE", arg "50000000"
05-17 17:15:42.368: I/System.out(637): DEBUG SMTP: Found extension "8BITMIME", arg ""
05-17 17:15:42.368: I/System.out(637): DEBUG SMTP: Found extension "STARTTLS", arg ""
05-17 17:15:42.368: I/System.out(637): DEBUG SMTP: Found extension "OK", arg ""
05-17 17:15:42.368: I/System.out(637): DEBUG SMTP: Attempt to authenticate
05-17 17:15:42.378: I/System.out(637): AUTH LOGIN

05-17 17:15:42.458: I/System.out(637): 334 VXNlcm5hbWU6

05-17 17:15:42.478: I/System.out(637): bWFyY29sYXBpbGFAbGliZXJvLml0

05-17 17:15:42.558: I/System.out(637): 334 UGFzc3dvcmQ6

05-17 17:15:42.603: I/System.out(637): bGFwaWxhODc4Nw==

05-17 17:15:42.688: I/System.out(637): 235 ... authentication succeeded

05-17 17:15:42.800: I/System.out(637): DEBUG SMTP: use8bit false
05-17 17:15:42.820: I/System.out(637): MAIL FROM:<xxxx@libero.it>

05-17 17:15:42.918: I/System.out(637): 250 <xxxx@libero.it> sender ok

05-17 17:15:42.918: I/System.out(637): RCPT TO:<xxxx@libero.it>

05-17 17:15:43.071: I/System.out(637): 250 <xxxx@libero.it> recipient ok

05-17 17:15:43.071: I/System.out(637): DEBUG SMTP: Verified Addresses
05-17 17:15:43.071: I/System.out(637): DEBUG SMTP:   xxxx@libero.it
05-17 17:15:43.071: I/System.out(637): DATA

05-17 17:15:43.148: I/System.out(637): 354 enter mail, end with "." on a line by itself

05-17 17:15:43.250: I/System.out(637): Sender: xxxx@libero.it

05-17 17:15:43.250: I/System.out(637): To: xxxx@libero.it

05-17 17:15:43.250: I/System.out(637): Message-ID: <1092772120.0.1431882939562.JavaMail.javamailuser@localhost>

05-17 17:15:43.258: I/System.out(637): Subject: This is Subject

05-17 17:15:43.258: I/System.out(637): MIME-Version: 1.0

05-17 17:15:43.258: I/System.out(637): Content-Type: text/plain; charset=us-ascii

05-17 17:15:43.258: I/System.out(637): Content-Transfer-Encoding: 7bit

05-17 17:15:43.258: I/System.out(637):

05-17 17:15:43.258: I/System.out(637): This is Body

05-17 17:15:43.258: I/System.out(637): .

05-17 17:15:43.358: I/System.out(637): 552 This message is not RFC 2822 compliant [smtp-34.iol.local; LIB_670]

05-17 17:15:43.358: I/System.out(637): DEBUG SMTP: got response code 552, with response: 552 This message is not RFC 2822 compliant [smtp-34.iol.local; LIB_670]
05-17 17:15:43.358: I/System.out(637): RSET

05-17 17:15:43.358: I/System.out(637): DEBUG SMTP: EOF: [EOF]
05-17 17:15:43.398: I/System.out(637): javax.mail.MessagingException: [EOF]
05-17 17:15:43.398: I/System.out(637):  at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
05-17 17:15:43.398: I/System.out(637):  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1512)
05-17 17:15:43.408: I/System.out(637):  at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1321)
05-17 17:15:43.408: I/System.out(637):  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:637)
05-17 17:15:43.418: I/System.out(637):  at javax.mail.Transport.send0(Transport.java:189)
05-17 17:15:43.418: I/System.out(637):  at javax.mail.Transport.send(Transport.java:118)
05-17 17:15:43.418: I/System.out(637):  at com.xxxx.sendmail.EMailSender$1.run(EMailSender.java:74)
05-17 17:15:43.418: I/System.out(637):  at java.lang.Thread.run(Thread.java:856)
05-17 17:15:43.418: I/System.out(637): QUIT

05-17 17:15:43.470: W/System.err(637): javax.mail.MessagingException: [EOF]
05-17 17:15:43.478: W/System.err(637):  at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
05-17 17:15:43.478: W/System.err(637):  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1512)
05-17 17:15:43.478: W/System.err(637):  at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1321)
05-17 17:15:43.488: W/System.err(637):  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:637)
05-17 17:15:43.501: W/System.err(637):  at javax.mail.Transport.send0(Transport.java:189)
05-17 17:15:43.508: W/System.err(637):  at javax.mail.Transport.send(Transport.java:118)
05-17 17:15:43.508: W/System.err(637):  at com.xxxx.sendmail.EMailSender$1.run(EMailSender.java:74)
05-17 17:15:43.508: W/System.err(637):  at java.lang.Thread.run(Thread.java:856)
« Ultima modifica: 17 Maggio 2015, 20:49:19 CEST da Pilatus »