我有一个用户输入电话号码的文本框,以及用户选择运营商的短信网关将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();
}
}
如果您需要从我的应用程序得到更多的代码,我非常乐意提供。
你会得到什么错误? –
发布一个[最小完整示例](http://stackoverflow.com/help/mcve)更快得到更好的帮助 – Reimeus