Sending Email with attachment using SMTP server with authentication
843830Jan 7 2004 — edited Feb 15 2004I have written a java application for sending email with attachments using smtp prototcol with authentication. I have configured the mail server for authentication.The problem is when i run the application it is throwing the following exception,
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 5.7.1 <mark@yahoo.com>... Relaying denied. Proper authentication required.
;
nested exception is:
javax.mail.SendFailedException: 550 5.7.1 <robin@javacommerce.com>... Relaying denied. Proper authentication required.
at javax.mail.Transport.send0(Transport.java:203)
at javax.mail.Transport.send(Transport.java:73)
at SendMailAuthentication.postMail(SendMailAuthentication.java:97)
at SendMailAuthentication.main(SendMailAuthentication.java:60)
Can you please help me in isolate the problem. My java application is follows,
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class AttachExample
{
private static String SMTP_AUTH_USER = "";
private static String SMTP_AUTH_PWD = "";
//String fileAttachment = null;
public static void main (String args[]) throws Exception
{
//try
// {
String emailhost = args[0];
String emailfrom = args[1];
String emailto = args[2];
String emailfileAttach = args[3];
SMTP_AUTH_USER = args[4];
SMTP_AUTH_PWD = args[5];
AttachExample attach = new AttachExample();
attach.sendMail(emailhost,emailfrom,emailto,emailfileAttach);
System.out.println("Message sent Succefully");
/* }
catch(Exception e)
{
System.out.println("USAGE : java AttachExample Hostname FromAddr ToAddr AttachementFile Username Password");
}*/
}
public void sendMail(String host ,String from,String to,String fileAttachment) throws MessagingException
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
// Get session
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props,auth);
session.setDebug(true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
// save output
PrintStream old = System.out;
// change output
System.setOut(ps);
// send
// reset output
System.setOut(old);
System.out.println("==================== DUBUG===================\n"+os);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("My E-Mail Attachment");
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
if(!fileAttachment.equals(" "))
{
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
message.setContent(multipart);
// Send the message
Transport trans = session.getTransport("smtp");
trans.connect(host,"SMTP_AUTH_USER","SMTP_AUTH_PWD");
Transport.send( message );
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
System.out.println("USER NAME :: "+username+", PASSWORD :: "+password);
return new PasswordAuthentication(username, password);
}
}
}