Results 1 to 15 of 15
Thread: JavaMail and Gmail
- 05-20-2010, 03:48 PM #1
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
JavaMail and Gmail
Hi!
The program of JavaMail to send email by using gmail SMTP server was working correctly in Windows 7. When I tested the program in Windows XP in a different machine, then the email is not going. Either Messaging or SendMail exception comes. I have used JDK1.6 and NetBeans 5.5 in both the machines.
Kindly let me know whether the problem is because of change of OS (in which case some changes in coding may be needed) or some setting needs to be done in NetBeans, which was taken automatically in Windows 7.
The code is=
Java Code:/* * SendMail.java * * Created on 19 April, 2010, 10:14 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package utility; import java.io.Console; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.*; /** * * @author Khushbu * @version */ public class SendMail { String username=null; String password=null; /** Creates a new instance of SendMail */ public SendMail() { } public void postMail( String recipients[ ], String subject, String message, String from, String pwd) throws MessagingException { boolean debug = false; //Set the host smtp address Properties props = new Properties(); //props.put("mail.smtp.host", "smtp.live.com"); String host="smtp.gmail.com"; props.put("mail.smtps.auth", "true"); // Authenticator a1= new PopupAuthenticator(); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address //InternetAddress addressFrom = new InternetAddress(from); //msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); //msg.setText(message); //Transport.send(msg); username = from; password = pwd; Transport t = session.getTransport("smtps"); try { t.connect(host, username, password); //t.connect(host, username); t.sendMessage(msg, msg.getAllRecipients()); } catch(Exception ex){ out.println(ex); } finally { t.close(); } } }
Last edited by khushbu; 05-21-2010 at 01:55 PM. Reason: code tags added
- 05-20-2010, 06:28 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Please use code tags when you posting again in the forum. Unformated codes are really hard to read. If you don't know how to do it, then please check on my forum signature. You can find the relevant link there.
- 05-20-2010, 06:29 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Regarding your question, did you comes with any error message?
- 05-20-2010, 08:57 PM #4
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Yes, the exception generated is:
javax.mail.MessagingException: can't determine local email addressLast edited by khushbu; 05-21-2010 at 01:49 PM.
- 05-21-2010, 03:26 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Please complete the complete error message you comes with. What you've post above is part of it. Complete error message tells you a lot.
- 05-21-2010, 05:24 PM #6
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Its the complete error message only that I have posted. This error message is displayed by following line:
out.println(ex);
- 05-22-2010, 01:22 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Rather print just the error message print the stack trace. It'll gives you lots of useful information.
Java Code:ex.printStackTrace();
- 05-22-2010, 01:23 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Here in the following line of code,
Java Code:t.connect(host, username, password);
- 05-22-2010, 03:10 PM #9
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Thanks for your reply. I've already checked the values and are all correct. "host" has got the value smtp.gmail.com, "username" has correctly got my gmail email-id and "password" has also got the password of my gmail account. I even tried ex.printStackTrace() but its displaying no output. If I replace out.print(ex) with ex.printStackTrace(), then a blank page appears.
I even added print statements after every line of code and it was seen that all the messages are displayed except the print statement just after t.connect() because the moment control comes to this line, exception is raised and it goes to the catch block.
I think that the issue is not really related to the code but of some setting in Win XP. Kindly help me in this asap.Last edited by khushbu; 05-22-2010 at 03:13 PM.
- 05-23-2010, 03:04 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
This is nothing to do with your OS.
javax.mail.MessagingException: can't determine local email address
- 05-23-2010, 03:05 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
- 05-23-2010, 03:07 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Is there any way that you can send me the complete code with all additional packages. So I can check it. Just looking at your code I cannot see any mistake you've done. Something wrong with the configurations.
- 05-23-2010, 06:41 PM #13
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
The code is complete. This method postMail() will be called with an array containing list of recipients, subject of the email, message of the email, from email-id of gmail and password of from gmail account. The jar files required by this mail are javamail-1.4.3 and jaf-1.0.2 (JAF is required if jdk1.5 or lower versions are used), which could be easily downloaded from google. Kindly let me know the way to transfer these files to you, if desired.
- 05-23-2010, 06:49 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Don't you've a ftp stream to transfer those files. Or else you can use one free file sharing service, such as 4shared.com - free file sharing and storage
- 05-23-2010, 06:49 PM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Anyway I'll try ti with your code tomorrow.
Similar Threads
-
error in smpt.gmail.com host
By kib_tse in forum NetworkingReplies: 15Last Post: 03-10-2011, 07:25 PM -
Java and gmail
By ak88 in forum Advanced JavaReplies: 1Last Post: 03-25-2010, 06:05 PM -
error in connecting to gmail server
By nyk976 in forum NetworkingReplies: 2Last Post: 03-10-2010, 03:36 PM -
Reading file attachments like gmail attachements
By janu.c in forum New To JavaReplies: 1Last Post: 08-05-2009, 08:07 AM -
530 5.7.0 Authentication Required - JavaMail gmail
By simon in forum Advanced JavaReplies: 1Last Post: 07-15-2007, 12:52 AM
Bookmarks