如何使用java向手机发送短信警报?

发布于 2024-09-07 05:14:01 字数 2083 浏览 13 评论 0原文

我正在开发一个简单的电子邮件应用程序,我已经使用 java 发送电子邮件,代码如下。`

public class SendMail {

    public SendMail() throws MessagingException {

        String host = "smtp.gmail.com";
        String Password = "mnmnn";
        String from = "[email protected]";
        String toAddress = "[email protected]";
        String filename = "C:/Users/hp/Desktop/Write.txt";
        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, toAddress);

        message.setSubject("JavaMail Attachment");

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Here's the file");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();

        } catch (SendFailedException sfe) {

            System.out.println(sfe);
        }
    }
}` 

我想使用 java 开发 SMSAlert 应用程序。我希望每当收到邮件时我的号码都会收到短信提醒。在java中可以吗?提前致谢。

Im developing a simple Email Application, I have done sending email using java with following code.`

public class SendMail {

    public SendMail() throws MessagingException {

        String host = "smtp.gmail.com";
        String Password = "mnmnn";
        String from = "[email protected]";
        String toAddress = "[email protected]";
        String filename = "C:/Users/hp/Desktop/Write.txt";
        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, toAddress);

        message.setSubject("JavaMail Attachment");

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Here's the file");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();

        } catch (SendFailedException sfe) {

            System.out.println(sfe);
        }
    }
}` 

I want to develope SMSAlert application using java. I want to get sms alerts to my number whenever i get mail. Is it possible in java. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-09-14 05:14:01

实现此目的的一种简单且免费的方法是将电子邮件发送到您的短信收件箱。

每个提供商都有不同的域,但假设发送到 [mynumber]@messaging.sprintpcs.com 的 sprint 电子邮件将发送给我。

似乎没有太多延迟,我们有监控进程,在错误情况下以这种方式发送电子邮件和文本,并且它们同时到达。

当然,如果您的收件人分布在多个网络中,维护此类系统就会变得更加棘手,因为您必须查找电子邮件域。

An easy and free way to accomplish this is to send an email to your sms inbox.

Each provider has a different domain, but say for sprint emails sent to [mynumber]@messaging.sprintpcs.com will be texted to me.

There doesn't seem to be much of a delay, we have monitoring processes that send out emails and texts this way on error conditions and they arrive simultaneously.

Of course if your recipients are spread across multiple networks maintaining this sort of system gets trickier, because you've got to look up the email domains.

梦巷 2024-09-14 05:14:01

已经有很多关于此的帖子了。

我的建议是通过第三方提供商,例如 aspsms.com,然后使用他们的 API(网络服务或无论如何)。

它有一个 .Net API,但通过 Java,您应该能够使用 SOAP Web 服务。

There have been quite a lot of posts about this already.

My advice is to go through a third party provider such as aspsms.com and then to use their API (web service or wathever).

This one has a .Net API, but with Java, you should be able to use the SOAP webservice.

晚雾 2024-09-14 05:14:01

请参阅此答案

有关 Kannel 和 SMS 网关的讨论线程可能会有所帮助。

See this answer

This discussion thread about Kannel and SMS Gateways might be of some help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文