用于从我的 Web 应用程序向印度的任何手机发送短信的 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7403407/
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
Java Code to send SMS from my Web Application to any Mobile in India
提问by
I have a requirement where I need to send SMS from my Web Application to any Mobile in India .
我有一个要求,我需要从我的 Web 应用程序向印度的任何移动设备发送 SMS。
I got below code from a ipipi.com website:
我从ippi.com 网站得到以下代码:
I want to implement this functionality, could anybody please help me what values are to be provided here:
我想实现这个功能,任何人都可以帮我在这里提供什么值:
String username = "YoureIPIPIUsername";
String password = "YourPassword";
String smtphost = "ipipi.com";
String compression = "Compression Option goes here - find out more";
String from = "[email protected]";
String to = "[email protected]";
String body = "Your Message";
SMTPSend.class
SMTPSend.class
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SMTPSend {
public SMTPSend() {
}
public void msgsend() {
String username = "YoureIPIPIUsername";
String password = "YourPassword";
String smtphost = "ipipi.com";
String compression = "Compression Option goes here - find out more";
String from = "[email protected]";
String to = "[email protected]";
String body = "Your Message";
Transport tr = null;
try {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
// Get a Session object
Session mailSession = Session.getDefaultInstance(props, null);
// construct the message
Message msg = new MimeMessage(mailSession);
//Set message attributes
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());
tr = mailSession.getTransport("smtp");
tr.connect(smtphost, username, password);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
SMTPSend smtpSend = new SMTPSend();
smtpSend.msgsend();
}
}
回答by mcfinnigan
If the code above is linking to a service that converts incoming Email messages to outgoing SMS messages, you presumably need to purchase credit as stated by Alex K.
如果上面的代码链接到将传入的电子邮件消息转换为传出的 SMS 消息的服务,则您可能需要购买 Alex K 所述的信用。
A better way to send SMSes would be to use SMSLib to interface with a cellular provider's SMSC. You also then need to make sure that provider is able to route SMSes to all cellular networks.
发送 SMS 的更好方法是使用 SMSLib 与蜂窝提供商的 SMSC 连接。然后,您还需要确保提供商能够将 SMS 路由到所有蜂窝网络。
回答by Chand Priyankara
Your question seems specific to some service provider/API. But I would like to give a generic answer here.
您的问题似乎特定于某些服务提供商/API。但我想在这里给出一个通用的答案。
Sending SMS need a SMS gateway to a SMSC. There are two simple methods[of-cause more can be]. You can either use your SIM card[SMS enabled], or use someone else's SIM card. SIM card provides connection to a SMS gateway through your service provider with SMSC.
发送短信需要一个短信网关到 SMSC。有两种简单的方法[原因更多可以]。您可以使用自己的 SIM 卡[启用短信功能],也可以使用其他人的 SIM 卡。SIM 卡通过 SMSC 的服务提供商提供与 SMS 网关的连接。
Either ways, sending SMS is not free.Others may say its LIKELY free, but at-least an advertisement or a long term business marketing strategy must be there.
无论哪种方式,发送 SMS 都不是免费的。其他人可能会说它的 LIKELY 免费,但至少必须有广告或长期的商业营销策略。
The way you are continuing is using someone else's SIM through an API provided by them. Since those have dedicated bulk SMS accounts, it may faster, multiple threaded, service. They should commercially helpful to you to get your problem sorted.
您继续的方式是通过他们提供的 API 使用其他人的 SIM 卡。由于那些有专用的批量 SMS 帐户,它可能会更快,多线程,服务。他们应该在商业上对您有帮助以解决您的问题。
If you want to send SMS from your SIM, you have to connect your SIM to computer and use an standard API to call SMS functionality. SMSLib
is what I suggest. You need a hardware to connect your SIM to PC, either connect your mobile to it or, use a Data Modem[easy] and interface through a COM
port.
如果您想从 SIM 卡发送 SMS,您必须将 SIM 卡连接到计算机并使用标准 API 调用 SMS 功能。SMSLib
是我的建议。您需要一个硬件来将您的 SIM 卡连接到 PC,或者将您的手机连接到它,或者使用数据调制解调器 [easy] 并通过COM
端口连接。
Try this code here, if you want to try your own SMS gateway.
如果您想尝试自己的 SMS 网关,请在此处尝试此代码。
回答by user914323
First create an account in ipipi.com
and validate it by clicking the activation link sent to the mail.And give your username as YoureIPIPIUsername and password as YourPassword.
首先在 中创建一个帐户ipipi.com
并通过单击发送到邮件的激活链接进行验证。并提供您的用户名作为 YoureIPIPIUsername 和密码作为 YourPassword。
String smtphost = "ipipi.com"
String compression = "Compression Option goes here - find out more";
There are some compression types hereif u don't want anything you can mention "None"
这里有一些压缩类型 ,如果你不想要任何你可以提到的“无”
String from = "[email protected]"
String to = "[email protected]";
Destination ph number should be +91 and the 10 digit number
目的地电话号码应为 +91 和 10 位数字
String body = "Your Message";