| Author |
sending sms
|
alec stewart stewart
Ranch Hand
Joined: Dec 23, 2003
Posts: 71
|
|
the following code gets compiled but gives a runtime error ststing: SMS was not sent - Sending failed; nested exception is: javax.mail.SendFailedException: Invalid Addresses; nested exception is: javax.mail.SendFailedException: 550 <name@domain.com>, Recipient unknown 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 public class EmailSMS { // Global Variables String TO; String FROM; String SUBJECT; String TEXT; String MAILHOST; String LASTERROR; // Main function executed public static void main(String[] args) throws Exception { EmailSMS SMS = new EmailSMS(); SMS.setMailHost("server"); SMS.setTo("name@domain.com"); SMS.setFrom("name@domain.com"); SMS.setSubject(""); SMS.setText("Hello World!"); boolean ret = SMS.send(); if (ret) { System.out.println("SMS was sent!"); } else { System.out.println("SMS was not sent - " + SMS.getLastError()); } } // Public Constructor public EmailSMS() { TO = null; FROM = null; SUBJECT = null; TEXT = null; MAILHOST = null; LASTERROR = "No method called."; } public void setTo(String to) { TO = to; } public String getTo() { return TO; } public void setFrom(String from) { FROM = from; } public String getFrom() { return FROM; } public void setSubject(String subject) { SUBJECT = subject; } public String getSubject() { return SUBJECT; } public void setText(String text) { TEXT = text; } public String getText() { return TEXT; } public void setMailHost(String host) { MAILHOST = host; } public String getMailHost() { return MAILHOST; } public String getLastError() { return LASTERROR; } // Will attempt to send the Email SMS and return a boolean meaning it // either failed or succeeded. public boolean send() { // Variables to check message length. int maxLength; int msgLength; // Check to make sure that the parameters are correct if (TO.indexOf("mobile.att.net") > 0) { maxLength = 140; } else if (TO.indexOf("messaging.nextel.com") > 0) { maxLength = 280; } else if (TO.indexOf("messaging.sprintpcs.com") > 0) { maxLength = 100; } else { maxLength = 160; } // Calculate message length msgLength = FROM.length() + 1 + SUBJECT.length() + 1 + TEXT.length(); // Typically, there are at least two characters of delimiter // between the from, subject, and text. This is here to make // sure the message isn't longer than the device supports. if (msgLength > maxLength) { LASTERROR = "SMS length too long."; return false; } // Set Email Properties Properties props = System.getProperties(); if (MAILHOST != null) { props.put("mail.smtp.host", MAILHOST); } // Get a Session object Session session = Session.getDefaultInstance(props, null); try { // Construct the email Message msg = new MimeMessage(session); // Set From if (FROM != null) { msg.setFrom(new InternetAddress(FROM)); } else { msg.setFrom(); } // Set Subject msg.setSubject(SUBJECT); // Set Text msg.setText(TEXT); // Add Recipient msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO, false)); // Sent Date msg.setSentDate(new Date()); // Send Email SMS Transport.send(msg); LASTERROR = "Success."; return true; } catch (MessagingException mex) { LASTERROR = mex.getMessage(); return false; } } } :roll:
|
 |
Stefan Wagner
Ranch Hand
Joined: Jun 02, 2003
Posts: 1923
|
|
That means, that 'name@domain.com' is unknown. Perhaps the domain doesn't exist, or the name isn't known at the domain. And could you use indentation (Code-Button). And strip off 187 setter - and getter - Methods. The exception should mention in which LINE the error occurs. You could mark that line. [Note that you can edit your posting to fix these problems.] [Edited for niceness by EJFH] [ February 21, 2004: Message edited by: Ernest Friedman-Hill ]
|
http://home.arcor.de/hirnstrom/bewerbung
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24039
|
|
|
Moving this to the "Other Java APIs" forum, where JavaMail questions are on-topic.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: sending sms
|
|
|