Difference between revisions of "Java Sending SMS via SMTP"

From SMS Wiki
Jump to: navigation, search
(New page: Required JAR files * mail.jar - from JavaMail - http://java.sun.com/products/javamail/downloads/index.html * activation.jar - http://java.sun.com/products/javabeans/jaf/downloads/index.ht...)
 
 
Line 1: Line 1:
Required JAR files
 
* mail.jar - from JavaMail - http://java.sun.com/products/javamail/downloads/index.html
 
* activation.jar - http://java.sun.com/products/javabeans/jaf/downloads/index.html
 
 
Be sure to set your classpath to the location where the files are stored.
 
 
 
<source lang="java">
 
<source lang="java">
 
+
import java.util.Date;
import java.io.*;
+
import java.net.InetAddress;
+
 
import java.util.Properties;
 
import java.util.Properties;
import java.util.Date;
 
import javax.mail.*;
 
import javax.mail.internet.*;
 
import javax.activation.*;
 
 
  
public class SMTPSend {
+
import javax.mail.Message;
 +
import javax.mail.MessagingException;
 +
import javax.mail.Session;
 +
import javax.mail.Transport;
 +
import javax.mail.internet.AddressException;
 +
import javax.mail.internet.InternetAddress;
 +
import javax.mail.internet.MimeMessage;
  
    public SMTPSend() {
+
public class SendSmsViaSmtp {
    }
+
  
    public void msgsend() {
+
/**
        String username = "YoureIPIPIUsername";
+
* @param args
        String password = "YourPassword";
+
* @throws MessagingException
        String smtphost = "ipipi.com";
+
* @throws AddressException
        String compression = "Compression Option goes here - find out more";
+
*/
        String from = "YoureIPIPIUsername@ipipi.com";
+
public static void main(String[] args) throws AddressException, MessagingException {
        String to = "DestinationPhoneNumber@sms.ipipi.com";
+
SendSmsViaSmtp smtpSend = new SendSmsViaSmtp();
        String body = "Your Message";
+
smtpSend.msgsend();
        Transport tr = null;
+
}
  
        try {
+
/**
        Properties props = System.getProperties();
+
* @throws AddressException
        props.put("mail.smtp.auth", "true");
+
* @throws MessagingException
 +
*/
 +
public void msgsend() throws AddressException, MessagingException {
 +
String username = "yourusername";
 +
String password = "yourpassword";
 +
String smtphost = "smtp.upsidewireless.com";
 +
String compression = ""; // insert compression option here if desired
 +
String from = "yourusername@smtp.upsidewireless.com";
 +
String to = "DestinationPhoneNumber@sms.upsidewireless.com";
 +
String body = "Your Message";
 +
Transport tr = null;
  
        // Get a Session object
+
Properties props = System.getProperties();
        Session mailSession = Session.getDefaultInstance(props, null);
+
props.put("mail.smtp.auth", "true");
  
        // construct the message
+
// Get a Session object
        Message msg = new MimeMessage(mailSession);
+
Session mailSession = Session.getDefaultInstance(props, null);
  
        //Set message attributes
+
// construct the message
        msg.setFrom(new InternetAddress(from));
+
Message msg = new MimeMessage(mailSession);
        InternetAddress[] address = {new InternetAddress(to)};
+
        msg.setRecipients(Message.RecipientType.TO, address);
+
        msg.setSubject(compression);
+
        msg.setText(body);
+
        msg.setSentDate(new Date());
+
  
        tr = mailSession.getTransport("smtp");
+
// Set message attributes
        tr.connect(smtphost, username, password);
+
msg.setFrom(new InternetAddress(from));
        msg.saveChanges();
+
InternetAddress[] address = { new InternetAddress(to) };
        tr.sendMessage(msg, msg.getAllRecipients());
+
msg.setRecipients(Message.RecipientType.TO, address);
        tr.close();
+
msg.setSubject(compression);
        } catch (Exception e) {
+
msg.setText(body);
            e.printStackTrace();
+
msg.setSentDate(new Date());
        }
+
    }
+
  
      public static void main(String[] argv) {
+
tr = mailSession.getTransport("smtp");
          SMTPSend smtpSend = new SMTPSend();
+
tr.connect(smtphost, username, password);
          smtpSend.msgsend();
+
msg.saveChanges();
      }
+
tr.sendMessage(msg, msg.getAllRecipients());
}  
+
tr.close();
 +
}
 +
}
 
</source>
 
</source>

Latest revision as of 14:44, 11 May 2007

import java.util.Date;
import java.util.Properties;
 
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendSmsViaSmtp {
 
	/**
	 * @param args
	 * @throws MessagingException 
	 * @throws AddressException 
	 */
	public static void main(String[] args) throws AddressException, MessagingException {
		SendSmsViaSmtp smtpSend = new SendSmsViaSmtp();
		smtpSend.msgsend();
	}
 
	/**
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public void msgsend() throws AddressException, MessagingException {
		String username = "yourusername";
		String password = "yourpassword";
		String smtphost = "smtp.upsidewireless.com";
		String compression = ""; // insert compression option here if desired
		String from = "yourusername@smtp.upsidewireless.com";
		String to = "DestinationPhoneNumber@sms.upsidewireless.com";
		String body = "Your Message";
		Transport tr = null;
 
		Properties props = System.getProperties();
		props.put("mail.smtp.auth", "true");
 
		// Get a Session object
		Session mailSession = Session.getDefaultInstance(props, null);
 
		// construct the message
		Message msg = new MimeMessage(mailSession);
 
		// Set message attributes
		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());
 
		tr = mailSession.getTransport("smtp");
		tr.connect(smtphost, username, password);
		msg.saveChanges();
		tr.sendMessage(msg, msg.getAllRecipients());
		tr.close();
	}
}