2014-02-24 75 views
0

我有一個用戶輸入電話號碼的文本框,以及用戶選擇運營商的短信網關將SMS電子郵件從Java應用程序發送到手機的組合框。我有沒有問題從一個文本框抓住一個字符串,但是當我使用,JComboBox到涉及SMS的字符串

String gateway = (String)comboBox_1.getSelectedItem(); 

,或者

String gateway = comboBox_1.getSelectedItem().toString(); 

我得到的錯誤和短信不會發。

這裏是我的代碼,涉及到的短信和部分下拉框中:

final String[] carriers = {"@txt.att.net", "@myboostmobile.com", "@messaging.sprintpcs.com", "@tmomail.net", "@vtext.com"}; 

...

JComboBox comboBox_1 = new JComboBox(carriers); 
    comboBox_1.setSelectedIndex(-1); 
    contentPane.add(comboBox_1); 
    comboBox_1.setRenderer(new PromptComboBoxRenderer("Select Carrier Gateway")); 
    ((JLabel)comboBox_1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER); 

    textField_1 = new JTextField(); 
    contentPane.add(textField_1); 
    textField_1.setColumns(10); 
    textField_1.setHorizontalAlignment(JLabel.CENTER); 

...

public class SMTPSend { 

    public SMTPSend() { 
    } 

    public void msgSafe() { 

     String number = textField_1.getText(); 
     String gateway = (String)comboBox_1.getSelectedItem(); 
     // alternatively tried .toString() 
     String username = "[email protected]"; 
     String password = "password"; 
     String smtphost = "smtp.gmail.com"; 
     String compression = "subject"; 
     String from = "[email protected]"; 
     String to = number + gateway; // where number is the 10 digit phone number and gateway is @SMS_Gateway 
     String body = "Hello World!"; 
     Transport myTransport = null; 

try { 
Properties props = System.getProperties(); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 

Session mailSession = Session.getDefaultInstance(props, null); 
Message msg = new MimeMessage(mailSession); 
msg.setFrom(new InternetAddress(from)); 
InternetAddress[] address = {new InternetAddress(to)}; 
msg.setRecipients(Message.RecipientType.TO, address); 
msg.setSubject(compression); 
msg.setText(body); 
msg.setSentDate(new Date()); 

myTransport = mailSession.getTransport("smtp"); 
    myTransport.connect(smtphost, username, password); 
    msg.saveChanges(); 
    myTransport.sendMessage(msg, msg.getAllRecipients()); 
    myTransport.close(); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 

如果您需要從我的應用程序得到更多的代碼,我非常樂意提供。

+1

你會得到什麼錯誤? –

+1

發佈一個[最小完整示例](http://stackoverflow.com/help/mcve)更快得到更好的幫助 – Reimeus

回答

1

我的猜測是,您沒有選擇任何一個運營商,因此當您詢問所選商品時,會返回null值。這個演示似乎有效 - 你能重現它的問題嗎?

import java.awt.event.*; 
import javax.swing.*; 

public class ComboBoxDemo extends JPanel{ 

    public ComboBoxDemo(){ 

     final JComboBox cb = new JComboBox(new String[]{"@txt.att.net", "@myboostmobile.com", "@messaging.sprintpcs.com", "@tmomail.net", "@vtext.com"}); 
     cb.setSelectedIndex(-1); 

     JButton button = new JButton("Print Selection"); 
     button.addActionListener(new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if(cb.getSelectedIndex() != -1) 
        System.out.println(cb.getSelectedItem()); 
       else 
        System.out.println("Not selected"); 
      }}); 


     add(cb); 
     add(button); 
    } 

    public static void main(String[] args){ 
     final JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxDemo()); 
     frame.pack(); 
     frame.setSize(400, 300); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

是的,我使用s.o.p進行調試,它們在選擇時都會打印。雖然發送短信但沒有運氣。 – user3308568

+0

@ user3308568這很可能意味着問題不在JComboBox中。你能用硬編碼的字符串重現問題嗎(擺脫所有的擺動組件)?如果是這樣,我會建議編輯你發佈只有這樣的例子。另外,如果拋出異常,堆棧跟蹤會有所幫助。 –

相關問題