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.

Relay

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 !

This entry was posted in ASP.NET, Out+of+control. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • penguin

    wanna see that captcha

  • Qatar guy

    Thanks Peter…it really helped me a lot

  • PeterGekko

    Thank you :)

  • reader

    captcha

  • Test

    sdfasdf

  • Das

    test

  • Anonymous

    :)

  • KeithBa

    I’m sure it wasn’t meant literally

  • harsha

    try this
    SmtpMail.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
    smtpmail.send(msg)

  • http://www.scorpiotek.com Christian Saborio

    @pvanooijen: All I have to say is that you sir, have the patience of a saint. It’s amazing how people simply put their question w/o reading if it had been previously addressed.

    It’s also amazing how people expect you solve their IT issues :D Thanks for posting this, it helped me immensely.

    PS: Your Captcha rocks!

  • Xebeche

    To all who have stucked mails in C:\Inetpub\mailroot\Queue.

    The probable reason is that your ISP blocks port 25. You have to set up your ISP’s SMTP server as Smarthost in your SMTP Virtual Server properties (tab Delivery -> Advanced)

  • Agaran Sankar

    Thank you so much for this valuable post. u had done a great Job.
    please Continue posting.
    Nandri

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    Your error message is clear. Isn’t it ?

  • http://www.dreambigtech.co.cc DreamBig

    I m getting this error: Mailbox unavailable. The server response was: 5.7.1 Message rejected as spam by Content Filtering.

    did as told int the post..but still the error comes?

    anyhelp…

  • http://nobovu.wordpress.com Christopher Union

    Thanks! That REALLY helped! I never would’ve thought to check IIS!

  • hewa

    Its working Thanks but the mails are not received do you why so.please help me.

  • http://www.myzonal.com MyZonal.com

    Thanks..its working but can we send mails from our smtp server without authentication or Credentials…?

    Thanks
    http://www.myzonal.com

  • Tay Leong Wei

    Thanks for your solution. And it’s really helped me.
    I only can get the email in C:\Inetpub\mailroot\Queue.
    Is there any idea , we can send the email to yahoo account ?

  • Johan

    Thanks!

  • http://sverma.it5@gmail.com sant

    u r really helped me. THNX

  • http://petersgekko.codebetter.com pvanooijen

    There’s notthing you can do about that, it’s up to Yahoo where in which folder they will delever the message. Do something about the senders address or the message content to convince their spam-filter ?

  • http://visoftltd@yahoo.com Toyin Fatuga

    Thanks, Mr. Peter. But, what can I do to prevent the mail be send to SPAM folder in Yahoo mail, instead of INBOX.

  • Toyin Fatuga

    Thank Men! I am short of Word

  • Ken Palmer

    Thanks Peter. That helped a ton.

  • http://blackleafdesigns.com tesfa

    I’ve been having this problem for days, and evently switched to using the pickup directory…that too was a headache.

    But now I found your post and it work…thanks a BILLION

  • Techkie

    Thank you so much Peter!! It’s the best.

  • Allwin

    Thank you very much, your Blog really helped me

  • http://shariffdotnet.blogspot.com shariff

    thanks the information was very helpfull!!

  • http://www.qxchange.com velmurugan

    thanks for ur notes.
    its nice to work from localhost.
    thanks once again.

  • Ryan J.

    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!!!!!

  • abcd

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

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    @ 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.

  • Raina

    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

  • JP Estrada

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

  • Shelon Padmore

    Interesting solution,

    - Shelon Padmore

  • Kishore

    Thanks for your tip. It really helped me.

  • Anthony

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

  • http://vineeth198312@yahoo.com vineeth babu

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

  • http://tiw_20062000@yahoo.com brajesh tiwari

    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);
    }

  • http://tiw_20062000@yahoo.com brajesh tiwari

    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);

  • saro

    thanks a lot man, greate job.

  • Bula

    Thanks!

  • Lapetra

    “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.

  • JimmyMac

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

  • Jame

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

  • Linga Reddy Sama

    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

  • http://www.synfosys.com Linga Reddy Sama

    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

  • Teju

    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

  • Satu

    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 ?

  • James

    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,

  • Monica Rossetti

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

  • Amal chacko

    In trying to send a simple MailMessage, I get

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

    What will be the problem?

  • shalini

    that worked for me, Thank you.

  • shalini

    Thank you, that worked for me.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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 ?

  • Jimbo

    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?

  • http://armaninfotech.com ammiraju

    Hey your code helped our team a lot thank you

  • http://www.mvtechs.com Mauricio

    Thanks Master, I’m done

  • Venkat

    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.

  • divya

    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

  • http://wss3.agileer.com Doug Seelinger

    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.

  • Jobi K John

    Is it possible to send smtp mail through proxy server?

  • Jobi K John

    In trying to send a simple MailMessage, I get
    Mailbox unavailable. The server response was: 5.7.1 Unable to relay for
    What will be the problem?

  • Pushpa kumr.M

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

  • http://www.calistomind.com Jay

    Thanks! Just what I was looking for…

  • vijay

    Saved me some grief

  • Tony Taylor

    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

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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.

  • Matthias

    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

  • I finally got this to work too

    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.

  • http://ashrafali_sh002 Ashrafali

    Thankyou very much.
    This information was verymuch useful it solved my problem.
    Once more thankyou

  • http://www.sharkinfotech.com kalubhai

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

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

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

  • shaikh

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

    help……………………………………………..

  • JReal

    Thank you

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

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

  • Ueslei

    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

  • Galina

    Thanks, just what I need to fix the problem.

  • abdu

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

  • PavelK

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

  • Shaan

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

  • jaic

    This information was verymuch useful it solved my problem

  • Latte

    Thanks anyway =)
    u are nice

  • http://codebetter.com/blogs/peter.van.ooijen/default.aspx pvanooijen

    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#!

  • Latte

    Oh ya, where can i find IIS log?

  • Latte

    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 !

  • http://codebetter.com/blogs/peter.van.ooijen/default.aspx pvanooijen

    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.

  • Latte

    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

  • http://codebetter.com/blogs/peter.van.ooijen/default.aspx pvanooijen

    @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.

  • PKaur

    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?

  • Ciro

    Thanx ;-)

  • Surjit

    Thanx peter…

  • Dan

    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.

  • Ross

    You are a lifesaver.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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.

  • fash

    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

  • schooltje

    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

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Agnes en schooltje:
    Is smtp on IIS really started ?
    Did you fill in the right computer/IP addres ? Try starting with “all except”

  • schooltje

    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?

  • http://Dirontsho@702mail.co.za Agnes

    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

  • http://yaprak ihsan

    thanks peter..

    iis configuration that i wasn’t find..

  • aderogba

    Thank you so much.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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

  • Sam

    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