Friday, April 18, 2014

How to Send SMS Using Java Applications


Getting Eclipse, Your Project, and JavaMail Ready
1. Download and install Eclipse by navigating to http://www.eclipse.org/downloads/ in your Web Browser. We will be using Eclipse to write our Java code and install the JavaMail library.
2. Navigate to the 'JavaMail Download' link in the Resources section. Click the blue 'Download' button in the middle of the page. Click the 'Agree to JavaMail License Agreement' check box and click 'Continue.' Click the 'javamail-1.4.3.zip' link. Choose to save the file instead of open it.
3. Unzip the 'javamail-1.4.3.zip' file by double-clicking it. Remember the location it unzipped to.
4. Open Eclipse. Click 'File,' 'New,' 'Java Project.' Type 'SMSText' as the project name and click 'Finish.'
5. Click the 'SMSText' folder in the Package Explorer (the toolbar on the left) and select 'Properties.'
6. Click 'Java Build Path' on the left then click the 'Add External JARs...' button on the right. Navigate to the unzipped 'JavaMail-1.4.3' folder and click 'Mail.jar' then click 'OK' and 'OK' again. The JavaMail library is now loaded for your SMSText project, and you'll be able to use it to send emails (and subsequently Text Messages) with Java.
Sending a Message
7. Second-Click the 'SMSText' project in Eclipse and select 'New,' 'Class.' Type 'MailClient' as the name and click 'Finish.'
8. Select all the code in the MailClient.java window and replace it by copy-pasting the following Java Code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class MailClient
{public void sendMail(String from, String to,
String subject, String messageBody) throws MessagingException, AddressException
{
// Setup mail server
String host = 'your_email_carriers_smtp';
String username = 'your_email@address.com';
String password = 'your_email_password';
Properties props = new Properties();
props.put('mail.smtps.auth', 'true');// Get a mail session
Session session = Session.getDefaultInstance(props, null);// Define a new mail message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);message.setText(messageBody);// Send the message
Transport t = session.getTransport('smtps');
try {
t.connect(host, username, password);
t.sendMessage(message, message.getAllRecipients());
} finally {
t.close();
}}public static void main(String[] args)
{
try
{
MailClient client = new MailClient();
String from='your_email_address';
String to = 'Recipients10DigitNumber@RecipientsCarriersSMTP.com';
String subject='Test';
String message='I'm testing. Do you see this?';client.sendMail(from,to,subject,message);
}
catch(Exception e)
{
e.printStackTrace(System.out);
}}
}
9. Navigate to the 'Email Carriers SMTP Addresses' link in the resources section. Find your email carrier, and copy the addresses. Select the text inside of the quotes of 'your_email_carriers_smtp' (next to 'String host =') and paste the address. For example, if you are using Gmail, the 'String host' line will read:
String host = 'smtp.gmail.com';
10. Replace 'your_email@address.com' with your email address.
11. Replace 'yourPassword' with the password you use to log in to your email. You will need this to let Java send an email from your email address. Don't worry, this information cannot be seen or sent anywhere over the web, and is necessary to send your text message; after all, the message has to be from somebody.
12. In the 'public static void main' method, replace the 'your_email_address' next to 'from =' with your email address.
13. Navigate to the 'Wireless Carriers SMTP Addresses' link in the Resources section. Look up the Wireless Carrier of your recipient's email address (or, if you'd like to test on your on phone, you can look up your own Wireless Carrier's address). For example, if your recipient's number is 555-555-5555 and he/she uses Verizon Wireless, the address will be '5555555555@vtext.com'.
14. Replace 'Recipients10DigitNumber@RecipientsCarriersSMTP.com' with the address you found in the previous step.
15. Change the 'Subject' or 'Message' to anything you'd like, or you can keep them as 'Testing' and 'I'm testing. Do you see this?'.
16. Hit the green play button to run your code. Nothing will be outputted to the console screen. The recipient should receive the text in a normal amount of time (as if you sent the text from your phone).

No comments:

Post a Comment