• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending sms

 
alec stewart stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the "Other Java APIs" forum, where JavaMail questions are on-topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic