【发布时间】:2011-04-06 14:07:10
【问题描述】:
谁能告诉我如何通过 Java Web 应用程序发送短信。 我看到了各种解决方案,但我没有得到它们。
谁能帮帮我。我不希望它仅限于 GSM 手机。
我试过这段代码,但它不起作用
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent("this is test mail", "text/plain");
message.setFrom(new InternetAddress("friendwithme18@gmail.com"));
message.setSubject("hello");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("phoneno@sms.gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
【问题讨论】:
-
How to send SMS using Java 的可能重复项
-
我在链接中看到了解决方案,但我无法得到它
标签: java