0
点赞
收藏
分享

微信扫一扫

java发送短信(转)


在把短信X连接到服务器COM1接口后,最好先用个java类来测试一下


    1. import java.util.ArrayList;   
    2. import java.util.List;
    3. import java.util.regex.Matcher;
    4. import java.util.regex.Pattern;
    5.
    6. import org .smslib .IOutboundMessageNotification;
    7. import org .smslib .OutboundMessage;
    8. import org .smslib .Service ;
    9. import org .smslib .Message.MessageEncodings;
    10. import org .smslib .modem.SerialModemGateway;
    11.
    12. /**
    13. * 短信发送测试类
    14. * @author mazq
    15. *
    16. */
    17. public class SMSUtil{
    18. public class OutboundNotification implements IOutboundMessageNotification
    19. {
    20. public void process(String gatewayId, OutboundMessage msg)
    21. {
    22. "Outbound handler called from Gateway: "
    23. System.out.println(msg);
    24. }
    25. }
    26. public void sendSMS(String mobilePhones,String content){
    27. // System.out.println(mobilePhones+"--"+content);
    28. Service srv;
    29. OutboundMessage msg;
    30. OutboundNotification outboundNotification = new OutboundNotification();
    31. srv = new Service ();
    32. // SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
    33. SerialModemGateway gateway = new SerialModemGateway( "modem.com1" , "COM1" , 115200 , "wavecom" , "9600"
    34. gateway.setInbound( true);
    35. gateway.setOutbound( true);
    36. "0000"
    37. gateway.setOutboundNotification(outboundNotification);
    38. srv.addGateway(gateway);
    39. "初始化成功,准备开启服务"
    40. try{
    41. srv.startService();
    42. "服务启动成功"
    43. ","
    44. for( int i= 0
    45. msg = new OutboundMessage(phones[i], content); //手机号码,和短信内容
    46. //这句话是发中文短信必须的
    47. srv.sendMessage(msg);
    48. " == "
    49. }
    50. srv.stopService();
    51. } catch(Exception e){
    52. e.printStackTrace();
    53. }
    54. }
    55. public static void main(String[] args) {
    56. SMSUtil util = new SMSUtil();
    57. "1355xxxxxxx" , "测试短信"
    58. }
    59.
    60. }



     
    如果收到短信,则说明ok了

    如果在日志中输出下面的内容,一般是说你的commons-net-1.4.1.jar或comm.jar没有配置到classpath中
    org .smslib .GatewayException: Comm library exception: java.lang.reflect.InvocationTargetException
     at org .smslib .modem.SerialModemDriver.connectPort(SerialModemDriver.java:93)
     at org .smslib .modem.AModemDriver.connect(AModemDriver.java:106)
     at org .smslib .modem.ModemGateway.startGateway(ModemGateway.java:111)
     at org .smslib .Service $1Starter.run(Service .java:227)

    举报

    相关推荐

    0 条评论