Error in forwading file to mail...
hello friends,
i am making one application. in that i have to forward file to mail through attechment. i have taken one example code but its not working properlly.
code is
import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;
public class AttachExample {
public static void main (String args[]) throws Exception {
String host = "smtp.kar.com";
String from = "FromAddre@kar.com";
String to[] = new String[]{"ABC@kar.com","XYZ@kar.com"};
String filename = "AttachFile.txt";
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
System.out.println(session.getProperties());
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
toAddress[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("Hello JavaMail Attachment");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Here's the file");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
try{
Transport.send(message);
}
catch(SendFailedException sfe)
{
message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses());
Transport.send(message);
}
}
}
i am getting followig error::::
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at AttachExample.main(AttachExample.java:49)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(Socke tFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFe tcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1359)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
can anybody help me
thanks in advance...