java手机pc,使用java从pc发送短信到手机

I have built one application to send sms using java and i googled a lot and finally implemented the following code but i am getting many exceptions that i have given below:

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();

}

} //

Program is running but in my mail box i found: Delivery to the following recipient failed permanently:

+91mymobilenumber@sms.gmail.com

How can i send sms using java?

It is urgently required as I have to show demo to my Project leader regarding how to send sms using java.

Any help is much appreciated

解决方案

please make some modifications in this variable

old :

String smtphost = "gmail.com";

replace it to

String smtphost = "smtp.gmail.com";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值