How do I send an email message from my C# application?

adeena picture adeena · Dec 14, 2008 · Viewed 11.5k times · Source

This is the code I wrote:

        MailMessage mail = new MailMessage("[email protected]", "[email protected]");

        mail.Subject = "This is a test!!";
        mail.Body = "testing...";

        SmtpPermission connectAccess = new SmtpPermission(SmtpAccess.Connect);
        System.Console.WriteLine("Access?  " + connectAccess.Access);

        SmtpClient client = new SmtpClient("mail.myurl.com", 2525);
        client.Send(mail);

It's not working. I get an exception at the line "client.Send(mail)" that says "Mailbox unavailable. The server response was (MYLOCALCOMPUTERNAME) [MY LOCAL IP]:3045 is currently not permitted to relay through."

connectAccess.Access does return "Connect" (I'm not sure if this was necessary... I added it in to start the troubleshooting process.)

Does this mean that my local machine has to be configured in some way? What about when I deploy my app to other peoples machines? Will there need to be local configuration there? I'm just looking to create a "Send Feedback" type of link from my application.

(Note: in my real application I am using my real email addresses in both the "to" and "from" and my smtp is really my smtp address at the place that hosts my url/website)

thanks!

-Adeena

Answer

adeena picture adeena · Dec 14, 2008

@ Michael: thanks for the link. It's very helpful.

I think I figured out my problem. I did need to add the login credentials after I created my "client" object. I added the following line:

 client.Credentials = new System.Net.NetworkCredential("myloginat+myurl.com", "mypassword");

(sorry - I have this habit that after I search for an answer on the web and through my manuals for 2 hrs, I finally break down and post the question and then 5 minutes later figure it out. :) I think the act of writing down the question helps me more than anything else)

So it's working... although I won't claim I understand everything about how and why it's working so I do expect to run in to some problems as I give my program to others to use. i.e., will everyone using the program that has an internet connection be able to open this smtp connection to my server? I don't know the answer to that... I'll have to wait, see, and learn some more.

Thanks! :)

-Adeena