Dear Paul,
Thank you for your reply, below is the code i used
It is working if i send to my company mail ids, but if i send any mail to
hotmail or yahoo or other mail ids it is saying error.
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
public final class MailerBean extends Object implements Serializable {
/* Bean Properties */
private
String to = null;
private String from = null;
private String cc = null;
private String bcc = null;
private String subject = null;
private String message = null;
public static Properties props = null;
public static Session session = null;
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}
public void setFrom(String from) {
this.from = from;
}
public void setCC(String cc)
{
this.cc = cc;
}
public void setBCC(String bcc)
{
this.bcc = bcc;
}
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.");
}
props = System.getProperties();
props.put("mail.smtp.host", "mail.andreconsultancy.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", <mail id>
;
props.put("mail.smtp.password", <password> );
props.put("mail.debug", "true");
// create some properties and get the default Session
Session session = Session.getInstance(props, null);
try {
MimeMessage message = new MimeMessage(session);
Calendar rightNow = Calendar.getInstance();
message.setSentDate(rightNow.getTime());
message.setRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
if(cc != null)
{
message.setRecipient(Message.RecipientType.CC, new InternetAddress(this.cc));
System.out.println("CC set method of mail class");
}
if(bcc != null)
{
message.setRecipient(Message.RecipientType.BCC, new InternetAddress(this.bcc));
System.out.println("BCC set method of mail class");
}
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;
}
}
Error dissplayed is
-----------------------
################## B 4 ###################
################## B 4 sendmail() ###################
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.5.0_09\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.5.0_09\jre\lib\javamail.address.map (The system cannot find the file specified)
BCC set method of mail class
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
java.lang.Exception
at MailerBean.sendMail(MailerBean.java:124)
at MailTest.callSendMail(MailTest.java:53)
at MailTest.main(MailTest.java:74)
################## 1/2 ter ###################
Hope this helps you to guide me.
Thanking you in advance,
Ganesh