gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:03:47 26 4
gpt4 key购买 nike

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

public class SendMail {

public SendMail() throws MessagingException {

String host = "smtp.gmail.com";
String Password = "mnmnn";
String from = "xyz@gmail.com";
String toAddress = "abc@gmail.com";
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中可以吗?提前致谢。

最佳答案

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

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

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

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

关于java - 如何使用java向手机发送短信警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3146738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com