• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending sms

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the "Other Java APIs" forum, where JavaMail questions are on-topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic