Unable to Send mail.
I am not able to send the mail with this code. It is giving the following error.
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 554 SPAM-Relay detected
Please help me..import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
publicclass TestMail{
// Sender, Recipient, CCRecipient, and BccRecipient are comma-
// separated lists of addresses;
// Body can span multiple CR/LF-separated lines;
// Attachments is a
///-separated list of file names;
publicstaticint Send
(String SMTPServer
,String Sender
,String Recipient
,String CcRecipient
,String BccRecipient
,String Subject
,String Body
,String ErrorMessage[]
,String Attachments){
// Error status;
int ErrorStatus = 0;
// create some properties and get the default Session;
Properties props = System.getProperties();
props.put("mail.smtp.host", SMTPServer);
System.out.println("Testing Mail Functionality1");
Session session = Session.getDefaultInstance(props,null);
try{
// create a message;
MimeMessage msg =new MimeMessage(session);
System.out.println("Testing Mail Functionality2");
// extracts the senders and adds them to the message;
// Sender is a comma-separated list of e-mail addresses as
// per RFC822;
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}
System.out.println("Testing Mail Functionality3");
// extract the recipients and assign them to the message;
// Recipient is a comma-separated list of e-mail addresses
// as per RFC822;
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}
System.out.println("Testing Mail Functionality4");
// extract the Cc-recipients and assign them to the
// message;
// CcRecipient is a comma-separated list of e-mail
// addresses as per RFC822;
if (null != CcRecipient){
InternetAddress[] TheAddresses = InternetAddress.parse(CcRecipient);
msg.addRecipients(Message.RecipientType.CC, TheAddresses);
}
System.out.println("Testing Mail Functionality5");
// extract the Bcc-recipients and assign them to the
// message;
// BccRecipient is a comma-separated list of e-mail
// addresses as per RFC822;
if (null != BccRecipient){
InternetAddress[] TheAddresses = InternetAddress.parse(BccRecipient);
msg.addRecipients(Message.RecipientType.BCC,TheAddresses);
}
System.out.println("Testing Mail Functionality6");
// subject field;
msg.setSubject(Subject);
System.out.println("Testing Mail Functionality7");
// create the Multipart to be added the parts to;
Multipart mp =new MimeMultipart();
System.out.println("Testing Mail Functionality8");
// create and fill the first message part;
{
MimeBodyPart mbp =new MimeBodyPart();
mbp.setText(Body);
// attach the part to the multipart;
mp.addBodyPart(mbp);
}
System.out.println("Testing Mail Functionality9");
// attach the files to the message;
if (null != Attachments){
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))){
// create and fill other message parts;
MimeBodyPart mbp =new MimeBodyPart();
FileDataSource fds =new FileDataSource(Attachments.substring(StartIndex,PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
System.out.println("Testing Mail Functionality10");
// last, or only, attachment file;
if (StartIndex < Attachments.length()){
MimeBodyPart mbp =new MimeBodyPart();
FileDataSource fds =new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
System.out.println("Testing Mail Functionality11");
}
// add the Multipart to the message;
msg.setContent(mp);
System.out.println("Testing Mail Functionality12");
// set the Date: header;
msg.setSentDate(new Date());
System.out.println("Testing Mail Functionality13");
// send the message;
System.out.println("SMTPServer :"+SMTPServer);
System.out.println("Sender :"+Sender);
System.out.println("Recipient :"+Recipient);
System.out.println("CcRecipient :"+CcRecipient);
System.out.println("BccRecipient :"+BccRecipient);
System.out.println("Subject :"+Subject);
System.out.println("Body :"+Body);
System.out.println("Attachments :"+Attachments);
Transport.send(msg);
System.out.println("Testing Mail Functionality13.1");
}catch (Exception e){
System.out.println("Testing Mail Functionality13.2");
System.out.println("Testing Mail Functionality13.3" + e);
//ErrorMessage[0] = MsgException.toString();
System.out.println("Testing Mail Functionality14");
//Exception TheException = null;
//if ((TheException = MsgException.getNextException()) != null)
//ErrorMessage[0] = ErrorMessage[0] + "\n" + TheException.toString();
System.out.println("Testing Mail Functionality15");
ErrorStatus = 1;
System.out.println("Testing Mail Functionality16");
}
System.out.println("Testing Mail Functionality17");
return ErrorStatus;
}
publicstaticvoid main(String args[])
{
System.out.println("Testing Mail Functionality");
TestMail sm=new TestMail();
int res;
try
{
res=sm.Send("smtp.intra.bt.com","aarti.mishra@bt.com","aarti.mishra@bt.com",null,null,"Error: Output xml not found","Record id: \n\nOutput xml not found.",null,null);
System.out.println("The returned value is " + res);
}
catch (Throwable t_)
{System.out.println("Error : ");
t_.printStackTrace();
}
}
}

