Skip to main content
Inspiring
March 10, 2012
Answered

cfmail and JavaMail

  • March 10, 2012
  • 1 reply
  • 6458 views

Hi All,

  My applicatoin needs to send some email notifications with some registration codes included to my customers. So they can register an account by using this code to verify that the owns the email addresses. In my local machine, I try to implement the email functionality. I encountered some problems, however. 5 hours are gone for such a beautify Saturday. I am alittle tired. So I come here for help again. Greatly appreciated for any help offered. Here are two of my questions.

1. cfmail does not work in Developer's edition?

   Of course cfmail tag is the simplest way to do emailing. In Administrator page I have entered "smtp.gmail.com" (without quotation) as the Mail Server, and my gmail user name and password as the authentication. Then I try cfmail tag in ColdFusion page. It does not work, neither does it display any error message. Also, in Administrator page I am not able to check "Verify mail server connection" checkbox. If I check it and click "submit changes" button, the checkbox remains unchecked and the check mark I put them disappear. I suspect that cfmail tag does not work in Developer edition, right?

2. Second question: I went ahead try to write a program myself using JavaMail. I have done so before in some Java project. So it did not take me long to reuse some code from before. After fully tested it, I moved the Java class files to C:\ColdFusion9\wwwroot\WEB-inf\classes. Then I invoked this Java class in ColdFusion page, the system gave me this error message below. I did try to find the solution myself. But it turned out to be far more complited than I thought. Someone suggests removing activation.jar file in C:\ColdFusion9\lib folder. But if I do this, my java program would not work at all. I know the error below is caused by different classloader version. Yet I have no idea how to and where to tweak it. Anyone has experience in this or related problems before? Much much Thanks!!

Error casting an object of type com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed.

com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler

    This topic has been closed for replies.
    Correct answer ffcai

    You are right Ffcai. If you include attributes in the cfmail tag that have already been set in the Administrator, then it shouldn't really matter. That is, so long as the attribute values are valid. The values in the cfmail tag overrule those of the Administrator (Hope I remember correctly).

    What you're witnessing might be just a tiny bug that needs ironing out. On the whole, cfmail is a tried and tested tool.

    The documentation says the only 3 required attributes are from, to and subject. So what happens when you set port, SSL use, server, and so on in the Administrator, and do just this:

    <cfmail

    from="fromAddress@gmail.com",

    to="toAddress@hotmail.com",

    subject="Eat my shorts, man!">

    Blah, blah, blah.

    </cfmail>


    I know where goes wrong. It has nothing to do with the mail server, User name, and password fileds. These fields are filled correctly. The problem is related to port number and SSL and TLS. First port 25 does not work for smtp.gmail.com. If you only set port 25 in administrator page, the page will complete succesffully but the mail won't go to your inbox. Instead I need to specify both port number 465 used and SSL checked, or both port number 587 used and TLS checked. I can't specify port 465 in the administrator page and enable SSl in ColdFusion page. This won't work. I never see things like this. Why I have to specify both in one place or another. CF is not smart enough to figure out.

    1 reply

    BKBK
    Community Expert
    March 11, 2012

    ffcai wrote:

    Hi All,

      My applicatoin needs to send some email notifications with some registration codes included to my customers. So they can register an account by using this code to verify that the owns the email addresses. In my local machine, I try to implement the email functionality. I encountered some problems, however. 5 hours are gone for such a beautify Saturday. I am alittle tired. So I come here for help again. Greatly appreciated for any help offered. Here are two of my questions.

    1. cfmail does not work in Developer's edition?

       Of course cfmail tag is the simplest way to do emailing. In Administrator page I have entered "smtp.gmail.com" (without quotation) as the Mail Server, and my gmail user name and password as the authentication. Then I try cfmail tag in ColdFusion page. It does not work, neither does it display any error message. Also, in Administrator page I am not able to check "Verify mail server connection" checkbox. If I check it and click "submit changes" button, the checkbox remains unchecked and the check mark I put them disappear. I suspect that cfmail tag does not work in Developer edition, right?

    As far as I know, CFmail does work on the Developer Edition of ColdFusion 9. I suspect the gmail settings in the Administrator had mistakes.

    I would advise you to try and fix this, rather than to bring in JavaMail. Did you check the logs? There will likely be something there.

    For a start, google: coldfusion smtp.gmail.com.You will see how others got it to work.


    2. .....I did try to find the solution myself. But it turned out to be far more complited than I thought. Someone suggests removing activation.jar file in C:\ColdFusion9\lib folder.

    Almost never a wise thing to do.

    ffcaiAuthor
    Inspiring
    March 11, 2012

    Good to see you again BKBK!

    I am a little frustrated. Now I can get the cfmail to work. But things are still weird. I copied this code snippet from inernet and replaced with my info. It works.

    <cfset mailAttributes = {

    server="smtp.gmail.com",

    username="myAddress@gmail.com",

    password="myPassword",

    from="fromAddress@gmail.com",

    to="toAddress@hotmail.com",

    subject="Eat my shorts, man!"

    }

    />

    <cfmail port="465" useSSL="true"

    attributeCollection="#mailAttributes#"

    >port 465, SSL enabled</cfmail>

    Now I am trying to modify my version according to the above. Surpirsingly, the below does not work. Everything is the same!

    <cfmail subject="#form.subject#"  from="fromAddress@gmail.com" to="#form.toAddress#"

                        username="myAddress@gmail.com" password="myPassword" useSSL="yes" port="465">

    #form.messageBody#

    </cfmail>

    Then I added one more attribute to the list: server="smtp.gmail.com". This make it happy! But I already have the Mail server, User name, and password in place in Administrator page. I should not need to provide them here. Surpisingly I have to. If I omit the username attribute, it gives me this error:

    This exception was caused by: javax.mail.AuthenticationFailedException: failed to connect.

    If I omit the password attribute, the page finished normally. No error shown. No error in application.txt log file either. But the email was not sent to my email address. Its like nothing happened.

    So I have to manually key in all these three attributes even though I have specified them in the Administrator. I have no idea what is going on...

    Also if I omit the useSSL attribute and change port number to 25, it does not work either. The page finished normally. But no errors at all. application.txt log file does not show any error too.

    I am glad I have a working version to use now. But I am frustrated about why the other version do not work. They should.

    Do you have any ideas?

    ffcaiAuthor
    Inspiring
    March 11, 2012

    So I have to manually key in all these three attributes even though I have specified them in the Administrator. I have no idea what is going on...

    1) You should review the documentation. Some attributes only work in combination with others. So if one of the required elements is missing all of the related settings in that group may be ignored.

    2) You should be using debug=true . That generates extremely detailed information. Note, the output is not sent to standard mail.log. See the documentation for the log file location.


    I read the cfmail doc already. It does not say anything helpful to my question. For example, I already filled in Mail server, User name, password in administrator page, why I have to specify them again in the cfmail tag? The same mail server, username, password, port number works in JavaMail, why it does not work in ColdFusion? Clearly it has nothing to do with gmail account settings, otherwise my JavaMail program would not work. I also used "verify mail server connection" checkbox in Administrator page to verify the connection between CF and gmail server, it told me "Connection Verification Successful."