aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Javamail with gmail issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Javamail with gmail issue" Watch "Javamail with gmail issue" New topic
Author

Javamail with gmail issue

Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
Hello everyone, I am a new user to this forum, but have viewed may posts for help for some time now.

My problem is understanding SMTP and POP3 using Javamail.

Currently, I have code set up using SMTP to send an email from a web page I created. While I test this page, I have the "TO" hard coded in, for I want any message from that page to be sent to a main recipient at the company, so customers can request orders/information from the company via the website I created for them.

This feature works fine, to an extent. After the site sends the message, instead of using the "FROM" email address that a customer would enter for a return address, the email has a return address from my gmail account. I'm not sure why the "FROM" gets overridden, but I have a feeling it may be that I'm using SMTP and not POP3. I have searched for tons of examples of SMTP and POP3 using Javamail, but none have helped.

Here is my code (note some was used from an example I found at http://www.stardeveloper.com/articles/display.html?article=2001101101&page=1):

Please let me know how to fix this issue so that when a customer gets to the page, requests a product, the mail will be sent to the companys main recipient address, but show the return address of the customer, and not the company's address as the return address (which I'm sure will happen if I made this live right now).

Thanks for any help!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Welcome to the Ranch.

A few things to help you make the best of this fourm:

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information. You can go back and change your post to add code tags by clicking the button on your post.

Also, please take the time to choose the correct forum for your posts. This forum is for questions on Servlets. For more information, please read this. This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

Yes, you have to use SMTP to send e-mail messages. POP3 is for retrieving them from your inbox, not for sending them.

I'm not sure I understand your requirements. Are you saying you want to send an e-mail message, but you want it to appear as if it came from somebody else? And are you saying that the setFrom() method doesn't do what you want?
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi,




Tha code above you have pass null value but there you have to pass object.

That auth object contains user name and password of sender....



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
Thank you for the quick responses! My code is in a Servlet, so I thought that's where it would go. I'll be sure to always use the code snippet here on out -- that's a nice feature indeed. Thanks for the tips!

Paul you are almost correct. I want the customers return email address that they'll input on the page to read as the return email address after the customer submits the email. But for some reason the setFrom() method does get overridden since the message is going through my gmail account and makes the return address as my gmail address instead.

When I launch this site soon, I don't want to change the email code to the company's authentication and have them receive emails from with a return address of their own company, not the customer's company since I think it's something with my code. Could it possibly be since I'm going through gmail? Maybe I just need a better understanding of how sending a message from a site really works.

Nishan, my site doesn't require any login since it's only an "advertising" site if you want to call it that. I will keep your idea in mind though.

I just want a customer to come to the site, send a message if they would like, have that message fall into the inbox of company, and be able to respond to that message from the return address provided by the customer (sorry, frustration). Any ideas on how that should be set up? Should I not go through my gmail for testing? Should I get some local mail server to do that work instead? Bah, I'm so confused...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
I think what you want is a "Reply To" header (There's a JavaMail method for that). Then it doesn't matter where the message was sent from, if you hit "Reply" in your email client, it's going to use the address in the "Reply To" header instead of the one in the "From" header.

And yes, GMail -and, I believe, most other 3rd party email services- won't allow you to specify arbitrary From addresses when you send mail through them.


Ping & DNS - my free Android networking tools app
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
Ulf, I had a feeling that was the case with gmail. Do you happen to know a work around for this then? I tried using the setReplyTo() and setSender() methods from the MimeMessage, but those didn't change the reply to after the message was sent -- the reply was still being sent to my gmail account. Is there a way to send messages and not have to go through a client of some sort, but send them to a client -- can you do that using Java/JSP/Servlets, etc.? Or is there another approach to this that I should look into?

I appreciate the help from everyone.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

You could try setting the "envelope address" of your messages. Here's a line of code which I use in one of my programs to do that:

Also, I see you're using the "default instance" Session from your application server's configuration. You might find it better to call getInstance() rather than getDefaultInstance(), since I vaguely recall not being able to change the properties of the default instance, or something like that.
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
I don't have a default "from" though, only a default "to". I want anyone who comes to the "Contact Us" page to input their email address, and after the message is sent, it'll recognize that email as a return address while the message was sent to only one default "to" address. I'm starting to think that since it's hard to explain, I must be doing something completely backwards and ill-advised in programming, haha.

The gist I'm getting from JavaMail is that it's used for single, to multiple recipients, and only one sender -- I want the opposite of that if possible.

Does anyone know some good examples/sites to help explain what I need from this site better? Should I use a different approach/language?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
You could check out the Aspirin library, which is an embedded JavaMail-capable mail server. If nothing else, it allows you to customize the message in all imaginable ways if need be, especially the ways that 3rd party servers prohibit.
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
Thank you for that, I'll definitely look into that and play around with it tonight. It does seem more flexible than JavaMail itself.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

Erik Helms wrote:I don't have a default "from" though, only a default "to". I want anyone who comes to the "Contact Us" page to input their email address, and after the message is sent, it'll recognize that email as a return address while the message was sent to only one default "to" address.

Right. So your "default from" is the e-mail address that was input from the Contact Us page. What's the problem with that?
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
That default "from" gets overridden through the authentication process of using my gmail for the smtp, and my gmail address then becomes the new "from" address when sent to the default "to" address. So when the company from the "to" address gets the message and clicks 'reply', it'll reply back to my gmail address, and not the address the original customer input from the Contact Us page since it gets overridden for some reason.

When I go to the use the company's mail server instead of my gmail, I'm afraid the same thing will happen, where when someone sends a message from the site, the return address will be the company's own address in their inbox, which would be like sending a message to them self it they were to reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

However... you've just mentioned that this is a "Contact Us" form like the ones seen on a zillion other websites. I haven't ever implemented one of these myself but if I did, I'm not sure I would use e-mail for sending the information to the owner of the site. And if I did, I wouldn't make a big deal about who the e-mail came from. At least I don't think so. As I said I haven't implemented one of those things so I don't know how they usually work. (And it seems you're in the same situation.)

I would be more likely to dump the information into a database and give the owner access to an application that allowed them to go through the database and generate e-mail responses based on the data stored there.
Erik Helms
Greenhorn

Joined: May 11, 2009
Posts: 7
Yeah, I'm trying to fully understand how it all works also. The company requested that this page sends a message, from a customer accessing the page, to a predetermined email address at their company so they can respond to the message and start emailing the customer back and forth, and possibly send attachments, etc. afterward. That seems pretty logical to me, but if you have a better idea, I'm all ears for it since it seems I'm getting no where with this current situation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javamail with gmail issue