我正在创建一个能够从 PC 向手机发送短信的程序。无论是使用在线网关还是使用加密狗发送短信都没有关系(但是,你知道,只有当我们使用加密狗时,发件人的原始号码才会出现,不是吗?)。我在互联网上找到的大多数示例都是在 2006-2010 年编写的,它们都使用 JavaMail 和 Java Communication API,而现在几乎没有通信 api,可能是 oracle 放弃了它。
我正在寻找一些最新的并且正在运行的技术。这是我的第一次尝试。我很高兴看到教程。谢谢
在做了几次 Try & Error 之后,我在一年前创建了以下代码,它工作正常。它使用 GSM 调制解调器发送短信。您的 GSM 调制解调器应该连接到您的 Comm 端口。您将需要 Comm.jar 库来运行此代码,您可以从http://llk.media.mit.edu/projects/picdev/software/javaxcomm.zip下载它
我的代码是触发AT 命令的简单程序版本。它只是在 GSM 调制解调器上触发 AT 命令来发送 SMS。
import javax.comm.*;
import java.io.*;
import java.util.*;
public class SimpleWrite implements Runnable, SerialPortEventListener
{
public void run()
{}
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "";
static char ch = '"';
static String dest = ch + "111111111" + ch; // 10 Digit Mobile Number.
static InputStream inputStream;
static SerialPort serialPort;
static OutputStream outputStream;
public void serialEvent(SerialPortEvent event)
{
switch (event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
try
{
while ( (line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch (IOException e)
{
System.err.println("Error while reading Port " + e);
}
break;
}
} //switch
}
public SimpleWrite(SerialPort serial)
{
try
{
inputStream = serial.getInputStream();
try
{
serial.addEventListener(this);
}
catch (TooManyListenersException e)
{
System.out.println("Exception in Adding Listener" + e);
}
serial.notifyOnDataAvailable(true);
}
catch (Exception ex)
{
System.out.println("Exception in getting InputStream" + ex);
}
}
public static void main(String[] args)
{
String line1 = "AT+CMGF=1\r\n";
String line2 = "AT+CMGS=" + dest + "\r\n";
String line3 = messageString + "\r\n";
//String line1 = "AT+CREG=2";
//String line2 = "AT+CGREG?";
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if ( portId.getName().equals("COM11"))
{
System.out.println("SMS Sending....Port Found");
try
{
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
SimpleWrite wr = new SimpleWrite(serialPort);
}
catch (PortInUseException e)
{
System.out.println("Port In Use " + e);
}
try
{
outputStream = serialPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("Error writing to output stream " + e);
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
}
try
{
outputStream.write(line1.getBytes());
outputStream.write(line1.getBytes());
outputStream.write(line2.getBytes());
outputStream.write(line3.getBytes());
outputStream.write(26);
outputStream.flush();
}
catch (Exception e)
{
System.out.println("Error writing message " + e);
}
}
}
}
}
/** show text in the text window
* @param Text text string to show on the display
*/
public static void showText(String Text)
{
System.out.println(Text);
}
}