Visualizzazione dei risultati da 1 a 6 su 6

Visualizzazione discussione

  1. #1

    JavaMail con Autenticazione

    Buonasera,
    sto cercando di creare un form che invii una mail utilizzando JavaMail.
    Sono riuscito a farlo funzionare con un server che non richiede autenticazione, ma adesso che mi serve inviare la mail con Gmail non funziona.
    Prima di scrivere qui ho cercato su mille siti e provato tutti gli esempi che ci sono online, ma nessuno ha funzionato. Il problema è che non mi esce nessun errore.

    Vi posto il mio codice completo, è un file .jsp. Grazie anticipatamente.

    codice:
    <%@ page import = " javax.mail.*" %>
    <%@ page import = " java.util.*" %>
    <%@ page import = " javax.mail.internet.*" %>
    <%@ page import = " javax.activation.*" %>
    
    
    <%!
    public class SMTPAuthenticator extends Authenticator
    {
       protected String username;
       protected String password;
    
    
       public SMTPAuthenticator(String username, String password)
       {
           this.username = username;
           this.password = password;
       }
    
    
       @Override
       protected PasswordAuthentication getPasswordAuthentication()
       {
           return new PasswordAuthentication(this.username, this.password);
       }
    }
    
    
    public class javamaildemo {
        String from = "mittente@gmail.com";
        String to = "destinatario@gmail.com";
        String subject = "Password Recovery";
        String bodyText = "messaggiooo";
        
        public javamaildemo() {
            try {
                
                Properties props = new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.auth", "true");
                props.put("mail.debug", "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");
                
                Authenticator auth = new SMTPAuthenticator("mittente@gmail.com", "Password");
                 
                Session session = Session.getInstance(props,auth);
                session.setDebug(true);
                
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(subject);
                message.setSentDate(new Date());
                
                //
                // Set the email message text.
                //
                MimeBodyPart messagePart = new MimeBodyPart();
                messagePart.setText(bodyText);
                            
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messagePart);
                // multipart.addBodyPart(attachmentPart);
                
                message.setContent(multipart);
                
                Transport.send(message);
    
    
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    } 
    
    
    %>
    
    
    <%
        
        javamaildemo mjm = new javamaildemo();
            
    %>
    Ultima modifica di LeleFT; 15-05-2015 a 11:58 Motivo: Aggiunti i tag CODE

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.