向企业短信平台的MySql数据库中插入消息
业务场景:企业A已建设内部短信发送平台,使用的数据库为MySql
使用方法:将待发消息插入到MySql数据库中的发件箱表中
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using MySql.Data.MySqlClient; namespace SMSTest { internal class Program { private static void Main(string[] args) { sendmess("132xxxxxxxx", "您好,这是我用程序写的测试,收到请回复+英文字符hello world!+双引号\"123456zxcvcb\""); } private static void sendmess(string mobie, string msm) { string usermobile = mobie; { var mycon = new MySqlConnection( "server=ip地址;user id=用户名;Password=密码;database=数据库;charset=gb2312"); mycon.Open(); Guid id = Guid.NewGuid(); string mes = msm; string mysql = @"insert into sms_outbox(sismsid,extcode,destaddr,messagecontent,reqdeliveryreport,msgfmt,sendmethod,applicationid,requesttime) values ('" + id + "','444','" + usermobile + "','" + mes + "',1,15,0 ,'444',NOW()) "; var cm = new MySqlCommand(mysql, mycon); try { int i = cm.ExecuteNonQuery(); mycon.Close(); } catch { mycon.Close(); } } } } }
注意:在数据库连接中要注明所使用的字符集“charset=gb2312”,不然发送的短信内容在手机中会显示为乱码

浙公网安备 33010602011771号