java 如何在java中发送和接收短信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17048971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:48:34  来源:igfitidea点击:

How to send and receive SMS in java?

javajava-mesmssms-gatewaysmslib

提问by Muhammed Refaat

I want my Java application to send and receive SMSwithout using any additional hardware devices and it must be free.

我希望我的 Java 应用程序能够在不使用任何额外硬件设备的情况下发送和接收SMS,而且它必须是免费的。

I made my search but all i found is titles, i found somethings like SMSLibbut at the other hand i didn't find tutorials or books to learn that.

我进行了搜索,但我找到的只是标题,我找到了类似SMSLib 的东西,但另一方面,我没有找到教程或书籍来学习它。

I also found that SMSLibcode but didn't understand:

我还发现了SMSLib代码但不明白:

Send Message/SMS Code

发送信息/短信代码

package SMSEngine;
import org.smslib.*;
class SendMessage
{
public static void sendMessage(String number, String message)
{ 
CService srv = new CService("COM4",9600,"huawei","E220");
try
{
srv.setSimPin("0000");
srv.setSimPin2("0000");
srv.setSmscNumber("");
srv.connect();
COutgoingMessage msg = new COutgoingMessage(number, message); 
msg.setMessageEncoding(CMessage.MessageEncoding.Enc7Bit);
msg.setStatusReport(true);
msg.setValidityPeriod(8);
srv.sendMessage(msg);
srv.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
}

Read Message/SMS Codes

阅读信息/短信代码

package SMSEngine;
import org.smslib.*;
import java.util.*;
class ReadMessages
{
static CService srv;
public static LinkedList receiveMessage()
{
LinkedList msgList = new LinkedList();
/*
To Check COM port Go in following path in Windows7
Control Panel\Hardware and Sound\Bluetooth and Local COM

*/
srv = new CService("COM4",9600,"huawei","E220");//"COM1", 57600, "Nokia", ""
try
{
srv.setSimPin("0000");
srv.setSimPin2("0000");
srv.connect();
srv.readMessages(msgList, CIncomingMessage.MessageClass.Unread);
srv.disconnect();
return msgList;
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
return msgList;
}
}

回答by Mike Dinescu

In order to send SMS messages you have two options: either use a gateway modem, or use a bulk service with an online API.

为了发送 SMS 消息,您有两种选择:要么使用网关调制解调器,要么使用带有在线 API 的批量服务。

SMSLib is only a library that makes it easierto interface with a gateway (hardware device) or with a bulk SMS provider. Either way, the library by itself is not enough.

SMSLib 只是一个库,可以更轻松地与网关(硬件设备)或批量 SMS 提供商进行交互。无论哪种方式,仅靠图书馆是不够的。

The code sample that you provided appears to try to use a gateway connected to a local serial port but since you don't have such a hardware device it's not going to work for you.

您提供的代码示例似乎尝试使用连接到本地串行端口的网关,但由于您没有这样的硬件设备,因此它不会为您工作。

回答by David Jashi

One way is to use SMS gatewayand send them like ordinary emails.

一种方法是使用短信网关并像普通电子邮件一样发送它们。

回答by Yeasir

"I also found that SMSLibcode but didn't understand"-

“我也发现了SMSLib代码但不明白”-

Assuming that you know java/object oriented programming, read through an online tutorial on smslibfor understanding the basics. May be you can start with this one http://smslib.org/doc/smslib/quickstart/

假设您了解 Java/面向对象编程,请阅读有关smslib的在线教程以了解基础知识。也许你可以从这个http://smslib.org/doc/smslib/quickstart/开始