Results 1 to 16 of 16
- 02-05-2009, 12:25 PM #1
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...Rakesh Mehta
- 02-05-2009, 04:00 PM #2
Go to GMail's web site and review their documentation. Many SMTP services don't accept connections on port 25, and I think gmail may required SSL/TLS. They seem pretty good about describing their requirements, including ports.
- 02-06-2009, 07:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I don't know about the port, but gmail needs SSL/TLS for connections.
- 02-07-2009, 05:47 AM #4
Reply
hello
if anybody knows some other code for that then plz give me ...Rakesh Mehta
- 02-07-2009, 07:43 AM #5
reply
Hello friends,
As you told gmail needs SSL/TLS for connections. i change my coding now its not giving error of port.. but another error
new code ::
================================================== ==
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 = "mail.gmail.com";
String from = "abc@gmail.com";
String to[] = new String[]{"abc@abc.com","abc@abc.com"};
String filename = "C:\\Documents and Settings\\Desktop\\trick.txt";
System.out.print("Step 1:: ");
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host","mail.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
// added below line
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
System.out.println("Step 2:: ");
Session session = Session.getDefaultInstance(props, null);
//Session session = Session.getInstance(props, null);
//final String username = "abc";
// final String password = "abc";
// Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password); }});
System.out.println(session.getProperties());
System.out.println("Step 3:: ");
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);
System.out.println("Step 4:: ");
message.setContent(multipart);
try{
Transport transport = session.getTransport("smtp");
System.out.println("Step 5:: in try");
transport.connect("smtp.gmail.com","abc","abc");
System.out.println("Step 6:: in try before mail:");
message.saveChanges();
Transport.send(message);
System.out.println("Step 7:: in try after mail:");
}
catch(SendFailedException sfe)
{
message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses());
Transport.send(message);
}
}
}
output and error is::
================================================== ===
Step 1:: Step 2::
{java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=..........
.........
........
bugreport.cgi, mail.smtp.host=mail.gmail.com, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=}
Step 3::
Step 4::
Step 5:: in try
Step 6:: in try before mail:
Exception in thread "main" javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
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:69)
Java Result: 1
BUILD SUCCESSFUL (total time: 8 seconds)
so any suggestion ??
thank for reply..Rakesh Mehta
- 02-08-2009, 02:01 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In simply for me, authentication fail, as the exceptions shows there. How did you authenticate it?
- 02-08-2009, 03:40 AM #7
Glancing at your code, it appears you commented out the code where you specify the user name and password in the Properties instance. That would certainly lead to an authentication error ;-)
The real question is, what do you put in these fields?
First, gmail won't let you use their service without creating an account, which seems reasonable. Create a gmail account, and use the user name and password from that.
- 02-08-2009, 11:26 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And one more thing too, to our thread starter. Please use the code tags when you posting code segments again. Un-formated codes are hard to read lol.
- 02-09-2009, 06:59 AM #9
Thanks to all..
Hey friend ,
thanks to all.
also i will mention code tags from next time.Rakesh Mehta
- 02-09-2009, 09:55 AM #10
Member
- Join Date
- Feb 2009
- Location
- philippines
- Posts
- 16
- Rep Power
- 0
is that you?
;)
;)
.................................................. .................................................. .......
- 02-09-2009, 09:56 AM #11
Member
- Join Date
- Feb 2009
- Location
- philippines
- Posts
- 16
- Rep Power
- 0
it seems that you are great.....
- 02-09-2009, 09:57 AM #12
Member
- Join Date
- Feb 2009
- Location
- philippines
- Posts
- 16
- Rep Power
- 0
steve11235
...
you seems great....
- 02-09-2009, 12:08 PM #13
thanks 4 that also...
:cool:Rakesh Mehta
- 02-09-2009, 12:09 PM #14
:)............
Rakesh Mehta
- 02-09-2009, 12:23 PM #15
Rakesh is it working now ?
i am the future
- 02-09-2009, 01:28 PM #16
code is working fine
yes rjuyal
it working fine ...
having some problem when we r sending pdf file but that is because of its size .. i will solve that problem.. main problem was wth line
props.put("mail.smtp.host","mail.gmail.com");
in that i have to write props.put("mail.smtp.host","smtp.gmail.com");
....and authentication..
Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password); }});
now code is working...:)Rakesh Mehta
Similar Threads
-
javamail exception: 557 mail head error
By allanwakes in forum Java ServletReplies: 2Last Post: 10-02-2008, 11:05 AM -
error in reading mail contents
By kib_tse in forum NetworkingReplies: 1Last Post: 08-22-2008, 12:14 AM -
Javax.mail.MethodNotSupported Exception in java mail api
By namarc in forum Advanced JavaReplies: 2Last Post: 05-05-2008, 06:01 AM -
Sending a mail with the local mail program
By thedude in forum Advanced JavaReplies: 2Last Post: 07-23-2007, 12:19 PM -
Mail error
By orchid in forum New To JavaReplies: 1Last Post: 04-22-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks