Wednesday, January 16, 2013

Relaying the email

I finished my last blog on a cliff-hanger: after getting HTML text, attachments and UTF-8 subject encodings to work properly, I discovered that there was a major problem with my program. As long as mail was being sent to an address with the same Internet provider as the SMTP definition - and the OP and I share an Internet provider - everything was ok, but as soon as I tried to send email to an address outside of the provider's domain, I received an error message about relaying and the mail was not sent.

It transpires that a few years ago, Internet providers tightened up their email protocols to prevent spam mail. I wasn't aware of this and blissfully continued to use code which worked in the past but not now. My first action was to switch providers and send email via Gmail, but this too required some changes. After more feverish googling, I discovered that almost certainly I wouldn't be able to send email via Gmail with Indy 9 (which comes with Delphi 7), but that changing to Indy 10 would solve my problem.

Trying to find a site from which I could download of Indy 10 was amazingly difficult; the Indy project is very coy about the downloads and I had to negotiate several screens before I eventually found what I was looking for. Very strange: I would have thought that they would have placed a link to the latest version on their home page. After downloading the new version and updating Delphi so it would use this new version (a procedure which took about an hour to my further surprise), I eventually got an Indy 10/Gmail demo to work! Hurrah! One 'gotcha' was that two units have to be linked manually into the project and no one told me this explicitly.

But then I had to convert the subject line to UTF-8, the body to HTML and add the attachments. Finally I received an answer to my query about encoding the subject line: the TIDMessage component which exists in both Indy 9 and Indy 10 has an event called 'OnInitializeISO', which exposes two fields, VTransferHeader and VHeaderEncoding. By setting one to 'UTF-8' and the other to 'B', the subject line is automatically encoded as UTF-8/Base64. Unfortunately, there is no help regarding this field, so as Randy Newman sang correctly, "There ain't no book you can read". Fortunately, there was "someone to tell you", which is how I learnt about this. This little tip would have saved me at least three hours of screwing around had I known about it beforehand.

I still haven't got 'non-connected' attachments working correctly but I think that I know what to do. The only problem left - and it's no small problem - is that the email which I sent from home to work arrived correctly but with no Hebrew text contained within. It looks like I'm going to have to encode all of the internal text (what would be the body of the email) to UTF8/Base64 before storing this in the email. This isn't really a problem, more of an annoyance.

As I've been writing the majority of this email series a couple of days after the events described, I always knew when writing how I solved the problems. Now I don't know whether I will solve the two remaining problems easily, but I am optimistic. There is a tutorial but I think that it could be improved; a complete demo program would certainly help. Fingers crossed.

I've been very busy during the day for the past few days which means that I've had to work hard at night on these email problems. One night I dreamt about encoding subject headers and last night I dreamt about setting up web sites for a Chinese girl and having problems with the encoding. Hopefully I'll be able to dream about more regular subjects in the near future.

No comments: