Difference between revisions of "Java Sending SMS via SMTP"
From SMS Wiki
(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: | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
<source lang="java"> | <source lang="java"> | ||
| − | + | import java.util.Date; | |
| − | import java. | + | |
| − | + | ||
import java.util.Properties; | 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(); |
| + | } | ||
| + | } | ||
</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(); } }