我已经构建了一个通过Java发送SMS消息的应用程序,但是在应用程序执行期间我遇到了很多异常(见下文):
package john;
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 = "mygmailuserid@gmail.com";
String password = "mygmailpassword";
String smtphost = "smtp.gmail.com";
String compression = "My SMS Compression Information";
String from = "mygmailid@gmail.com";
String to = "+91mymobilenumber@sms.gmail.com";
String body = "Hello SMS World!";
Transport myTransport = null;
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
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());
myTransport = mailSession.getTransport("smtp");
myTransport.connect(smtphost, username, password);
msg.saveChanges();
myTransport.sendMessage(msg, msg.getAllRecipients());
myTransport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
SMTPSend smtpSend = new SMTPSend();
smtpSend.msgsend();
}
} //
应用程序正在运行,但在我的邮箱中,我收到了以下内容:
Delivery to the following recipient failed permanently:
+91mymobilenumber@sms.gmail.com
如何使用Java代码/库发送短信?
答案 0 :(得分:4)
Gmail不提供发送公共短信的网关。
发送短信通常需要花钱。
检查此链接,它可以证明是有用的:
以下是有关同一问题的另一个问题的链接:
答案 1 :(得分:3)
请对以下变量进行一些修改:
String smtphost = "gmail.com";
将其替换为:
String smtphost = "smtp.gmail.com";