java - JComboBox 转字符串涉及短信

标签 java swing sms jcombobox

我有一个文本字段,用户在其中输入电话号码,还有一个组合框,用户在其中选择其运营商 SMS 网关以将电子邮件从 java 应用程序发送到手机。我可以轻松地从文本字段中获取字符串,但是当我使用时,

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@gmail.com";
      String password = "password";
      String smtphost = "smtp.gmail.com"; 
      String compression = "subject"; 
      String from = "email@gmail.com";
      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();
  }
}

如果您需要我的应用程序中的任何更多代码,我非常乐意提供。

最佳答案

我的猜测是您没有选择运营商之一,因此当您请求所选项目时将返回 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);
    }
}

关于java - JComboBox 转字符串涉及短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999925/

相关文章:

java - Apache Tomcat 应用程序的默认 session 超时

java - HTTP 状态 500 - 未能延迟初始化角色 : . 的集合 ..,无法初始化代理 - 无 session

java - 从 JTable 中删除行 - Java

android - 在默认的 Android 文本应用程序中使用 SMSManager 显示出站文本

java - android另一个类sendSMS并在Mainactivity中调用错误

java - 尝试在 Android 中创建应用程序校验和,但它没有改变

Java JAR 内存使用 VS 类文件内存使用

java - 当输入或从 JFileChooser 中选择带有整数的文件时,我需要弹出 Jlist

java - 应用MVC模式的时候让controller继承view可以吗?

sms - 以编程方式接收短信