Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Checking the mail status after sending it ??

843830Mar 13 2004 — edited Mar 14 2004
Hi every body :-
I'm in my first steps in Java Mail and I need your helps.....
I'm using the following class to send mails to a certain reciepent ...It worked actually when the reciever mail address was correct (within my company domain)...but when I tried the following I failed can u please give ur suggestions ...
1)when I tried to send to wrong reciever in the correc domain, it gives no exception ....
2)when I want to send to external domain(@yahoo.com) it raises exception ==>550 5.7.1 Unable to relay for XXX
I want to be able to catch all of the exceptions ..but I don't know how ......

******************************************
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;

public final class MailerBean {
/* Bean Properties */
private String to = "wrong_reciever@Correct_Domain";
private String from = "test@Correct_Domain";
private String subject = "My Mail Program";
private String message = "Hi How are you..........";
public static Properties props = null;
public static Session session = null;

static {
/* Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "mail server IP Address");
session = Session.getDefaultInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}

public void setFrom(String from) {
this.from = from;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setMessage(String message) {
this.message = message;
}
/* Sends Email */
public void sendMail() throws Exception {
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
Transport.send(message);
} catch (MessagingException e) {
throw new Exception(e.getMessage());
}
}

/* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if((this.to == null) || (this.from == null) ||
(this.subject == null) || (this.message == null))
return false;

if((this.to.indexOf("@") == -1) ||
(this.to.indexOf(".") == -1))
return false;

if((this.from.indexOf("@") == -1) ||
(this.from.indexOf(".") == -1))
return false;

return true;
}
public static void main(String ss[])
{
MailerBean mb = new MailerBean();
try{ mb.sendMail();}
catch(Exception ex1)
{
System.out.println("Farah: Exception====> "+ex1.getMessage());
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2004
Added on Mar 13 2004
4 comments
443 views