Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [Java] Problema con java mail

    ho questo metodo per inviare le mail riempiendo i campi in un JFrame:
    codice:
    public class Mail {
    
        public static void send(String smtp, String to[], String cc[], String subject, String text) throws AddressException, MessagingException {
            Properties prop = System.getProperties();
            prop.put("mail.smtp.host", smtp);
            Session session = Session.getDefaultInstance(prop, null);
            Message msg = new MimeMessage(session);
            
            InternetAddress addForm = new InternetAddress("matteo.ferrone@gmail.com");
            msg.setFrom(addForm);
    
            InternetAddress[] addTo = new InternetAddress[to.length];
            for (int i = 0; i < addTo.length; i++) {
                addTo[i] = new InternetAddress(to[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addTo);
    
            if (cc.length > 0) {
                InternetAddress[] addCc = new InternetAddress[cc.length];
                for (int i = 0; i < addCc.length; i++) {
                    addCc[i] = new InternetAddress(cc[i]);
                }
                msg.setRecipients(Message.RecipientType.CC, addCc);
            }
    
            msg.setSubject(subject);
            msg.setContent(text, "text/plain");
            msg.setSentDate(null);
            Transport.send(msg);
        }
    }
    il problema è che se nn gli passo anche un CC mi dice "Illegal address".
    quindi o gli passo un CC o commento tutta questa parte:
    codice:
            if (cc.length != 0) {
                InternetAddress[] addCc = new InternetAddress[cc.length];
                for (int i = 0; i < addCc.length; i++) {
                    addCc[i] = new InternetAddress(cc[i]);
                }
                msg.setRecipients(Message.RecipientType.CC, addCc);
            }
    nel jframe ci stanno la jtext field per il TO e quella per il CC che vorrei poter lasciare anche vuota.

  2. #2
    nessuno sa dirmi come fare un controllo sull'array?
    nel jframe ho messo così:
    codice:
        private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {                                        
            try {
                String[] to = txtTo.getText().split(",");
                String[] cc = txtCc.getText().split(",");
                Mail.send(txtSmtp.getText(), to, cc, txtSubject.getText(), txtText.getText());
                JOptionPane.showMessageDialog(this, "Mail inviata!");
                txtTo.setText("");
                txtCc.setText("");
                txtText.setText("");
                txtSubject.setText("");
                comboTo.setSelectedIndex(0);
                comboCc.setSelectedIndex(0);
            } catch (AddressException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } catch (MessagingException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,326
    Direi che, banalmente, dovresti controllare che la textbox non sia vuota, prima di creare l'array:

    codice:
    String[] cc = new String[0];   // Parto dal presupposto di non avere CC
    if ( !"".equals( txtCc.getText() ) ){
       cc = txtCc.getText().split(",");
    }
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

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.