my dog learned polymorphism*
The moose likes Java Micro Edition and the fly likes Sending SMS from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Sending SMS from Java" Watch "Sending SMS from Java" New topic
Author

Sending SMS from Java

jagannadha reddy
Greenhorn

Joined: Jul 02, 2010
Posts: 14
HI,

I need to send SMS to client from my application. For this i followed sending SMS from email by using below code.


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 = "username";
String password = "password";
String smtphost = "smtp.gmail.com";
String compression = "My SMS Compression Information";
String from = "username@gmail.com";
String to = "mobilenumber@serviceprovider.com";
String body = "Hello SMS World!";
Transport myTransport = null;

try {
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port","465");
//props.put("mail.smtp.starttls.required", "true");
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();
}
}


It compiled successfully, but while running this code getting below exception:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. s29sm10871689wak.14

What to do to resolve this?
Sorry for the big post.
Please help me to complete this, it is very urgent.

Thanks&Regards
Jagan.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
How do I access Gmail with JavaMail?


Ping & DNS - my free Android networking tools app
Gopinath Karyadath
Ranch Hand

Joined: Oct 14, 2009
Posts: 87
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
What classes to import and what jar files to include?

Thanks,

Maki Jav


Help gets you when you need it!
jagannadha reddy
Greenhorn

Joined: Jul 02, 2010
Posts: 14
Hi Tried with above code, it is going to exception block and printing message as follows:

javax.microedition.io.ConnectionNotFoundException: sms
at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnec
tor.java:185)
at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.
java:162)
at javax.microedition.io.Connector.open(Connector.java:83)
at Javaronch.Sms(Javaronch.java:12)
at Javaronch.main(Javaronch.java:36)
Unable to connect to Station because of network problem

Please tell me the solution to resolve this and make it work successfully.

Regards
Jagan.
Gopinath Karyadath
Ranch Hand

Joined: Oct 14, 2009
Posts: 87
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending SMS from Java
 
Similar Threads
JSP using javamail
JavaMail: Help with sending e-mails
Java Mail send message to Yahoo account
JavaMail AuthenticationFailedException