2010-06-30 72 views
0

即时通讯开发一个简单的电子邮件应用程序,我已经做了发送使用Java电子邮件与以下code.`如何使用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中可能吗?提前致谢。

+0

请参阅[如何从Java发送短信?](http://stackoverflow.com/questions/545441/how-can-i-send-an-sms-from-java) – 2010-06-30 05:47:41

+0

试试这个:http: //stackoverflow.com/search?q=[java]+sms – 2010-06-30 05:48:58

回答

0

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

我的建议是通过第三方提供商,如aspsms.com,然后使用他们的API(Web服务或观察者)。

这个有一个.Net API,但是使用Java,你应该可以使用SOAP web服务。

1

完成此操作的一种简单而自由的方法是将电子邮件发送到您的短信收件箱。

每个服务提供商都有不同的域名,但是对于发送给[mynumber]的短信邮件,@ messaging.sprintpcs.com将发送给我。

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

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