从我的 Web 应用程序向印度的任何手机发送短信的 Java 代码

标签 java jakarta-mail

我有一个要求,我需要从我的 Web 应用程序向印度的任何移动设备发送 SMS。

我从 ipipi.com website 得到以下代码:

我想实现这个功能,谁能帮我在这里提供什么值:

String username = "YoureIPIPIUsername";
String password = "YourPassword";
String smtphost = "ipipi.com";
String compression = "Compression Option goes here - find out more";
String from = "YoureIPIPIUsername@ipipi.com";
String to = "DestinationPhoneNumber@sms.ipipi.com";
String body = "Your Message";

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 = "YoureIPIPIUsername@ipipi.com";
        String to = "DestinationPhoneNumber@sms.ipipi.com";
        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();
      }
}

最佳答案

如果上面的代码链接到将传入的电子邮件消息转换为传出的 SMS 消息的服务,您可能需要购买信用,如 Alex K 所述。

发送 SMS 的更好方法是使用 SMSLib 与蜂窝提供商的 SMSC 交互。然后,您还需要确保提供商能够将 SMS 路由到所有蜂窝网络。

关于从我的 Web 应用程序向印度的任何手机发送短信的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7403407/

相关文章:

java - 并发频率计数器-并发问题

java - 在 scala 中使用 java 格式化程序

java - Apache Velocity 中的错误或隐藏功能

java - 如果我在 Comparator.compare() 中总是返回 0,为什么我的 Set 中最终只有 1 个项目?

email - 如何配置 GlassFish JavaMail 以使用 Amazon SES?

java - 如何在一个线程中监听不同账户的大量IMAP文件夹?

java - 我可以在 hibernate 中使用相对路径指示映射文件吗?

java - 在 Java 中使用 POP3 根据指定的日期时间检索电子邮件

JavaMail API 和 Tomcat 7 实现

java - 使用 Java Mail 下载附件