tags:

views:

67

answers:

3

Hello. I try to do a program which sending emails. At the begining it works fine - I get 220(means "Hi"), then when I say "MAIL FROM:" it returns 250 (means OK) but then when I write "RCPT TO:" I get the 550 error and it writes "Relaying not allowed". why is google's smtp doesn't like it?

My code: http://pastebin.com/wNA1Zp26

*by the way: when I send the an email from another account - it doesn't work to.

+2  A: 

Including what language you are using would be a big plus.

Generally speaking though, no halfway decent email server/service is going to let you send any emails until you authenticate with the server/service. That's why you get the 'Relaying not allowed'.

Tony Abrams
So how can I identify with the server?
Ohad
I'm not a big sockets guy, particularly in c++, but here is a link that looks to give the command and responses for authenticating - http://computer-programming-tutorials.suite101.com/article.cfm/sending_email_with_smtp_auth
Tony Abrams
thanks.It's exactly what I did, and it's freaking out!"•Tell the server who the mail is from;•Tell the server who the mail is to;•Give the server the message;•Terminate with '\r\n.\r\n'."It doesnt tell me I don't send the right messages - it's just don't relay the message and I don't know why(!).
Ohad
Hmm ... check this out and see if it helps any - http://stackoverflow.com/questions/58210/c-smtp-example
Tony Abrams
@Tony: Nope, that's no help: It's essentially what the OP is doing, and it doesn't handle authentication.
Carl Smotricz
+3  A: 

I think the real error is that you're trying to implement a mail client. I'm no expert on C# or .NET, but for Java there is a "standard" library that does SMTP and all the other mail protocols, and I'd be very surprised if there weren't such a library in .NET.

Rather than wasting your time re-inventing socket communications with an SMTP server, you should educate yourself on the proper library to use for this purpose.

Or is this an academic exercise?

EDIT: Oops, it's C++, not C#. Blame my eyesight.

Here's a whole page full of links to SMTP libraries. Pick one, download it, use it.

Seriously, if you ever manage upon a link on how to do ESMTP authentication then you can probably manage the simpler forms of the basic "secret" handshakes that are required. But slapping together some code to handle the "happy path" and perhaps just talk to one particular SMTP server implementation is not the same as creating code that works securely and reliably, with decent error handling and reporting.

Carl Smotricz
But I'm not working with .NET, I use C++.
Ohad
Oops! My mistake, I took a brief look at the code and thought I saw "C#". I've updated my answer.
Carl Smotricz
A: 

A relaying error typically means that your client is trying to send an email to a recipient that is outside the system that you are connected to, but your client is not authorized to do so. Looking at your code, you are not attempting to authenticate yourself to the server at all.

For one thing, you are using the HELO command, and you are using it incorrectly at that. The input parameter to HELO is YOUR MACHINE NAME, but you are sending the SMTP SERVER HOSTNAME instead. You need to fix that.

After that, you should use the EHLO command instead of the HELO command. EHLO will allow your client to determine which authentication schemes (amongst other features) the server actually supports (such as LOGIN, NTLM, SHA1, MD5, etc). Then you can pick one that you support and use it to log in to an actual user account that has relaying permissions.

Another problem with your code is that it is not doing any SMTP-level error handling at all. You are looking at socket error codes, but you are not looking for SMTP error codes at all. For instance, if the server rejects the MAIL FROM or RCPT TO command (which it is in this situation), your code is still going to waste bandwidth and processing time sending a DATA command that will fail (with another SMTP error you are not looking for).

Remy Lebeau - TeamB