CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Using localhost as mailserver (5.7.1 Unable to relay for xxx)

With .NET 2.0 writing code which uses the IIS webserver to send mail has become even more powerful. The System.Net.Mail namespace has some rich classes.

private static void SendMailWithIIS(string subject, string body, string to)

{

    MailMessage message = new MailMessage();

    message.From =  new MailAddress("Me@Spammer.net");

    message.To.Add(to);

    message.Subject = subject;

    message.Body = body;

    message.BodyEncoding = System.Text.Encoding.ASCII;

    message.IsBodyHtml = true;

    message.Priority = MailPriority.Normal;

 

    SmtpClient smtp = new SmtpClient("Localhost");

    smtp.Send(message);

}

The .NET docs even contain a sample to send mail asynchronous. The hard part lies not in the code but in the configuration of localhost. It considers itself being used as a mail relay and by default it does not allow anyone to do that. In your code you'll get the error message : Mailbox unavailable. The server response was: 5.7.1 Unable to relay for

To fix it you have to configure relay restrictions in the IIS admin.

Here I've set localhost, aka 127.0.0.1, as the only one allowed to relay mail. And now my .NET code can spam everybody. Use with care !



Comments

Dennis van der Stelt said:

Some things are so simple, you'd expect everyone to know these things and use them wisely. Unfortunatly,...
# April 5, 2006 9:38 AM

Peter's Gekko said:

In a recent post I described how easy it was to turn your website into a spamming tool provided you know...
# April 7, 2006 6:03 AM

Sam said:

Thanks man...you r the one who help me to solve this problem, Microsfot development is SUCKS....always changes ..Never give what the hell we need to change ..its really Fting..

Keep posting good stuff


# April 11, 2006 4:56 PM

pvanooijen said:

Thanks Sam. But I'm not saying it's bad. It is indeed changing, from totally insecure to better secured.

What is bad is the miscommunication, which keeps us developers searching and searching
http://codebetter.com/blogs/peter.van.ooijen/archive/2006/04/07/142484.aspx
# April 12, 2006 4:34 AM

aderogba said:

Thank you so much.
# April 13, 2006 11:55 AM

ihsan said:

thanks peter..

iis configuration that i wasn't find..
# April 14, 2006 9:22 AM

Agnes said:

I have done all the steps in asp.net and  configured relay restrictions in the IIS admin but i still cant send sms.I still get the error 550 5.7.1 Pls help
# April 21, 2006 9:28 AM

schooltje said:

I have also done all the steps (just like "Agnes") but the message 'Mailbox unavailable. The server response was: 5.7.1 Unable to relay for 'emailadres' is coming. Does somebody have a solution?
# April 24, 2006 1:04 PM

pvanooijen said:

Agnes en schooltje:
Is smtp on IIS really started ?
Did you fill in the right computer/IP addres ? Try starting with "all except"
# April 24, 2006 4:26 PM

schooltje said:

Is smtp on IIS really started ? --> Yes

Did you fill in the right computer/IP addres ? --> Yes

Try starting with "all except" --> I have changed the radio button from 'only the list below' to 'all except the list below' and then the local SMTP server works. Why and How i don't know, but it works now:) Thnx
# April 25, 2006 5:39 PM

fash said:

Hi,
I have gone through and followed all the steps in here but do not see why I can not send emails. When debugging, it passes the line which is
' Send mail
       mailClient.Send(mailMessage)
but no email is being sent ??
Thanks
# April 26, 2006 9:33 AM

pvanooijen said:

Looks like the mail server has accepted the message. To find out what happened to it you should check the mail server's logs and the like. Your code has done its best.
# April 28, 2006 4:00 AM

Peter's Gekko said:

In two previous post I described my first steps in setting up my own internet server.

Part 1 described...
# June 6, 2006 7:47 AM

Ross said:

You are a lifesaver.
# August 7, 2006 11:07 AM

Dan said:

I get the same error message.
I'm using my local machine running XP Pro and localhost within Visual Web Developer to test an ASP.NET 2.0 application to see if I can run the asp:PasswordRecovery control.  So I don't have IIS installed on my local PC.  Is there any way I can get around this for my local testing.
# August 21, 2006 11:03 PM

Surjit said:

Thanx peter...
# August 24, 2006 5:37 AM

Ciro said:

Thanx ;-)

# August 25, 2006 2:25 AM

PKaur said:

I configured my SMTP settings and I dont receive any error message. But the mail is not being sent. What should I do about it?

# August 28, 2006 11:40 AM

pvanooijen said:

@Dan,

you need IIS. That's your mailserver. Cassini (vs 2005 webserver) doesn't have one.

@PKaur,

check you IIS log. That should tell you what went wrong.

# September 4, 2006 3:29 AM

Latte said:

Hi,

i copy and paste all your codes and edit a little.

protected void Button1_Click1(object sender, EventArgs e)

   {

       MailMessage message = new MailMessage();

       message.From = new MailAddress("Me@Spammer.net");

       message.To.Add("testing123@hotmail.com");

       message.Subject = "Hello";

       message.Body = "Hi there!";

       message.BodyEncoding = System.Text.Encoding.ASCII;

       message.IsBodyHtml = true;

       message.Priority = MailPriority.Normal;

       SmtpClient smtp = new SmtpClient("Localhost");

       smtp.Send(message);

   }

I realised i can only send mail when

1) The checkbox "Allow all computer which successfully auth.. .. " is checked.

2)message.From = new MailAddress("Me@Spammer.net");

 i can only use "Me@Spammer.net" and not other e-mail address .

 If i use other e-mail,No e-mail will be send.

please guide me..

thanks in advance

# October 26, 2006 10:32 AM

pvanooijen said:

Hard to say.

- Does localhost map to 127.0.0.1 ?

- Is there no other restriction set elsewhere ? In the firewall?

What puzzles me is that only the fake email address works. Doe sthe name you use map to an existing user ? In the latter case it points to "something else" fiddling with the permissions on your machine.

# October 28, 2006 2:08 PM

Latte said:

Actually I doing project in my school and under school network ,i cannot even send out the mail .. (Perhaps my school block it or smth. . )

I was actually keep changing the IP in the relay . . from 127.0.0.1 to my own ip address..

i got two things to clarify..

Under the Delivery tab(of SMTP option) i click on advance..

Under domain should i type in localhost or the domain(properties of my computer, computer name tab,the domain stated there ? ) of my computer?

One last thing is this statement,

SmtpClient smtp = new SmtpClient("Localhost");

do i need to change the content of "Localhost" to my ip such as  "192.168.0.3"?

or just leave it as "localhost" ?

Sorry if u find my questions annoying as i just started with c# project this semester ,that's why a lot of things i dont really understand . .

Have a nice day !

# October 28, 2006 9:43 PM

Latte said:

Oh ya, where can i find IIS log?

# October 28, 2006 11:08 PM

pvanooijen said:

You bet school has blocked ending mail. They don't want to be blacklisted as a spam source :)

Concerning your questions. As they get much broader than the scop of this post: may I suggest the MSDN docs. You'll get a much broader view there and learn a lot lot more then from my comments.

Enjoy your course, B#!

# October 30, 2006 1:52 AM

Latte said:

Thanks anyway =)

u are nice

# October 30, 2006 9:17 AM

jaic said:

This information  was verymuch useful it solved my problem

# November 1, 2006 8:00 AM

Shaan said:

The Solution for the Error "Mailbox unavailable. The server response was: 5.7.1 Unable to relay for (name)  "  is working  Thanks a lot......

# November 28, 2006 11:51 PM

PavelK said:

As for so many: It solved my problem. Thanks a lot ...

# December 18, 2006 5:42 AM

abdu said:

I have done all the things listed above, but it is still not working .i got same error

# January 3, 2007 2:45 AM

Galina said:

Thanks, just what I need to fix the problem.

# January 4, 2007 3:54 PM

Ueslei said:

Hi!

I receive the following message:

THE E-MAIL ADDRESS USED TO SEND MESSAGES IS NOT THE SAME AS THE ADDRESS THAT YOU AUTHENTICATED WITH (#5.7.1)

When sending message. My from address is a no-reply@mydomain.com.br (does not exists on the server).

Does anyone knows how to help me figure out this ??

Ueslei

# January 17, 2007 10:34 AM

pvanooijen said:

This requires some fiddling with permissions. Beyond my working knowledge. Googling around with your error message does provide usefull hints.

# January 18, 2007 2:37 AM

JReal said:

Thank you

# January 29, 2007 3:31 AM

shaikh said:

what can do to remove 5.7.1 error message box.......

help.....................................................

# January 30, 2007 2:16 AM

pvanooijen said:

Prevent it appearing ? As described above. And if when it apperas click OK ?

# January 30, 2007 2:25 AM

kalubhai said:

i have above code and i have complete send mail soccesfully but not recieve mail.

# February 4, 2007 10:29 PM

Ashrafali said:

Thankyou very much.

This information  was verymuch useful it solved my problem.

Once more thankyou

# February 15, 2007 3:38 AM

I finally got this to work too said:

But I tried setting the relay restrictions as described and it wouldn't work with 127.0.0.1  and the option Only the list below.  When I changed it to the option All except the list below it deleted the 127.0.0.1 from the computer list so there is nothing there now and it works fine.  The other option on the Relay Restrictions page for Allow All computer ... at the bottom is unchecked too.

# February 28, 2007 4:19 PM

Matthias said:

First of all.. thanks a lot the fix helped me as well, but I have another problem. I don't receive the mail although I was told that the mail was succesfully send.

2nd question .. I'm going to export my project from localhost to a real life asp.net server. Is this going to work on that server to with "localhost" as server describtion or do I have to change it to the server's IP Address?

Thanks a lot..

Regard Matthias

# March 2, 2007 3:39 AM

pvanooijen said:

I can't analyze your mail traffic from a distance ..

Localhost is just an alias for the 127,0,0,1 ip address. When the entry is in the lmhost file of the server it will work. Else not.

# March 2, 2007 6:55 AM

Tony Taylor said:

Hi

In trying to send a simple MailMessage under the SmtpClient, I get

Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Vers

ion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I've Googled and read the MSDN docs about the SmtpPermission class (which says the SmtpClient class demands an SmtpPermission - what does that mean?), but no luck.

Any ideas?

Thanks

# March 5, 2007 7:49 AM

vijay said:

Saved me some grief

# April 5, 2007 12:09 AM

Jay said:

Thanks! Just what I was looking for...

# April 10, 2007 11:47 PM

Pushpa kumr.M said:

Hai it's very good  this is programs is working in my local machine but how to develop in my real product server.

# April 19, 2007 3:13 AM

Jobi K John said:

In trying to send a simple MailMessage, I get

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for <xxx>

What will be the problem?

# April 23, 2007 9:00 AM

Jobi K John said:

Is it possible to send smtp mail through proxy server?

# April 23, 2007 9:04 AM

Doug Seelinger said:

Why isn't this more plainly stated in the documentation.  I understand that the "old way" in 1.1 wasn't as secure, but this little how-to should have been in the MS docs - or if it is, easier to find.

# May 12, 2007 1:08 AM

divya said:

Hi,This is not what i was looking for// what i needed is,

If i click on a link button say "Contac Us" a window for outlook should open up with the email address i specify in the code.

Can u help

# May 25, 2007 5:49 AM

Venkat said:

My mails are always dumped in the "Queue" folder, which means my SMTP is working fine.

But thereon the mails are never sent to the intended recipients.

Please help.

# May 30, 2007 11:38 AM

Mauricio said:

Thanks Master, I'm done

# May 31, 2007 9:06 PM

ammiraju said:

Hey  your code helped our team a lot thank you

# June 26, 2007 5:47 AM

Jimbo said:

Hi

I've followed all the instructions right at the top of this page.  The emails appear to get sent (i.e. my code doesn't break) however the emails aren't received by the recipient I specify (the recipient happens to be me for testing purposes).

Here's the code that executes succesfully:

SmtpClient theClient = new SmtpClient("localhost");

theClient.Send(message);

The emails aren't within any of the sub folders of C:\Inetpub\mailroot.

Any ideas where they are?

# July 4, 2007 4:32 PM

pvanooijen said:

That's beyond the scope of this code. Looks like there is a problem with receiving emails. Does the email-box accept mail from other senders ?

# July 5, 2007 5:33 AM

shalini said:

Thank you, that worked for me.

# July 10, 2007 4:08 PM

shalini said:

that worked for me, Thank you.

# July 10, 2007 4:09 PM

Amal chacko said:

In trying to send a simple MailMessage, I get

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for <xxx>

What will be the problem?

# July 26, 2007 1:46 AM

Monica Rossetti said:

This was the only straighforward explanation and solution to the "unable to relay" error. Thanks a lot

# August 21, 2007 6:14 PM

James said:

Have anybody tried to send emails to an internet email account, such as gmail or hotmail?

I am able to send emails to domain accounts but not with internet email accounts.

Thanks,

# August 23, 2007 3:40 PM

Satu said:

My error was this

CDO.Object didnt found. check if your configuration allow smtp.

it highlight the error at mailClient.Send(mailMessage)

did i got confuguration error ?

# September 10, 2007 8:49 PM

Teju said:

Hello,

I am using the simple code to send emails but I am getting the  Mailbox unavailable error.

As per your instructions  i tried to configure the IIS.

I have a setting under IIS which is set to allunassigned IP . If this is the case how can i set to asp.net to send emails.

Any help will be much apprceiated.

Regards,

Teju

# September 12, 2007 4:45 AM

Linga Reddy Sama said:

I have configured Default SMTP as you told above,but iam getting Exception as Mailbox unavailable. The server response was: 5.7.1 Unable to relay for sama.lingareddy@gmail.com

here sama.lingareddy@gmail.com this is from address

# October 19, 2007 6:48 AM

Linga Reddy Sama said:

My code is this,but iam getting Exception as Mailbox unavailable. The server response was: 5.7.1 Unable to relay for sama.lingareddy@gmail.com,iam trying to send an email from the Default SMTP server,Suggest me.

Private Sub SendMailWithIIS()

       Dim Message As New MailMessage

       Message.From = New MailAddress("sama.lingareddy@gmail.com")

       Message.To.Add("sama.lingareddy@gmail.com")

       Message.Subject = "OmSaiRam"

       Message.Body = "Om sairam"

       Message.BodyEncoding = System.Text.Encoding.ASCII

       Message.IsBodyHtml = True

       Message.Priority = MailPriority.Normal

       Dim smtp As New SmtpClient("Localhost")

       smtp.Send(Message)

   End Sub

# October 19, 2007 6:50 AM

Jame said:

but now i want to send to yahoomail but how to send to yahoo mail please hepl me?

# October 27, 2007 4:52 AM

JimmyMac said:

Thanks alot, I've been looking for this solution for months.

JiommyMac

# December 16, 2007 12:05 PM

Lapetra said:

"And now my .NET code can spam everybody. Use with care !"

It's not that I need to send spams but not sure how it is a spam method.

Email details will show a real IP address anyway.

# December 29, 2007 3:06 PM

Nathan Li’s Blog » Blog Archive » Mailbox unavailable. The server response was: 5.7.1 Unable to relay for x@x.com said:

Pingback from  Nathan Li&#8217;s Blog  &raquo; Blog Archive   &raquo; Mailbox unavailable. The server response was: 5.7.1 Unable to relay for x@x.com

# January 26, 2008 2:21 AM

Bula said:

Thanks!

# January 31, 2008 1:39 PM

saro said:

thanks a lot man, greate job.

# February 14, 2008 5:45 PM

brajesh tiwari said:

i have written the code below but it simply redirect the mail to  C:\Inetpub\mailroot\Queue rather then sending to  the person i want can some one help me

MailMessage message = new MailMessage();

       message.From = new MailAddress("Me@Spammer.net");

       message.To.Add("tiw_20062000@yahoo.com");

       message.Subject = "gfgdfg gdf gdfg dgf gdg bvc bcv b";

       message.Body = "cvx xcv xvx vc cvx v vcx xc v vx x ";

       message.BodyEncoding = System.Text.Encoding.ASCII;

       message.IsBodyHtml = true;

       message.Priority = MailPriority.Normal;

       SmtpClient smtp = new SmtpClient("192.168.10.79",26);

       smtp.Send(message);

# February 16, 2008 12:12 AM

brajesh tiwari said:

the code below simply redirect to C:\Inetpub\mailroot\Queue and doesn't send to the person i want

{

  MailMessage message = new MailMessage();

  message.From = new MailAddress("Me@Spammer.net");

  message.To.Add("tiw_20062000@yahoo.com");

   message.Subject = "gfgdfg gdf gdfg dgf gdg bvc bcv b";

    message.Body = "cvx xcv xvx vc cvx v vcx xc v vx x ";

    message.BodyEncoding = System.Text.Encoding.ASCII;

     message.IsBodyHtml = true;

      message.Priority = MailPriority.Normal;

      SmtpClient smtp = new SmtpClient("192.168.10.79",26);

      smtp.Send(message);

}

# February 16, 2008 12:17 AM

vineeth babu said:

thanks its working that is it dosent get an error but the mail is not received please send another option

# February 27, 2008 10:59 AM

Anthony said:

THANK YOU!  This was a huge help for such a small issue!

# March 4, 2008 11:25 AM

Kishore said:

Thanks for your tip. It really helped me.

# March 11, 2008 11:59 AM

Shelon Padmore said:

Interesting solution,

- Shelon Padmore

# April 26, 2008 10:53 AM

JP Estrada said:

I'm having the same problem as Brajesh.. my mails are stuck in the queue folder.. how do i fix this?

# May 7, 2008 12:20 AM

Raina said:

I have tried with the above code, but still i'm getting error like Message Sending Failure. We r using GoDaddy server. what is the solution

# May 10, 2008 3:43 AM

pvanooijen said:

@ Brajesh and all others having problems with the quueue of their server:

This code posts the mail messag eto the queue. After that it's in the hands of the mailserver and out of the scope of this post.

Contact your sysadmin to get it fixed.

# May 13, 2008 3:44 AM

abcd said:

hey but the mail is not delivering..... Is there be any firewall problem.... how to overcome that?

# June 25, 2008 2:42 AM

Ryan J. said:

Thank you so much... now I can develop and test on my dev box and it still works for our production server.  What a pain!!!!!

# June 27, 2008 11:16 AM

velmurugan said:

thanks for ur notes.

its nice to work from localhost.

thanks once again.

# August 7, 2008 1:35 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications