Step 1. Download Mail.jar and Activation.jar (see Resources for links) and save them in the Java library directory on the local drive of your computer.
Step 2
Run the new Java class in the Java Integrated Development Environment (IDE) and name it "MyMobileJava.java".
Step 3
Enter the following Java libraries at the beginning of your Java class. These libraries include the necessary Java Mail and Communications API resources and other supporting I / O and Internet class libraries for sending SMS text messages.
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.*;
Step-4 Place the following Java code after the library import statements to create an instance of the Java class and assign default SMS text message values.
public class SMTPSend { public SMTPSend() { } public void msgsend() { String username = "MySMSUsername"; String password = "MyPassword"; String smtphost = "MySMSHost.com"; String compression = "My SMS Compression Information"; String from = "mySMSUsername@MySMSHost.com"; String to = "PhoneNumberToText@sms.MySMSHost.com"; String body = "Hello SMS World!"; Transport myTransport = null;
Step-5 Create Java code to create a new communication session, which will then be used to configure the information contained in the text message. This information will then be prepared for dispatch. Enter the following Java code in your Java class at the end of the code you entered in step 4.
try { Properties props = System.getProperties(); props.put("mail.smtp.auth", "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());
Step-6 Send a text message by connecting to your SMS host address, save the changes in the message and then send the information. To do this, enter the following Java code to end the Java class.
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(); } }