views:

561

answers:

2

Hi all,

For about a year now, I've had problems trying to send and receive email programmatically using Visual C#. Not a single example out of hundreds that I've found on the web have ever worked. And no, I don't just copy and paste. I study the code, and modify/add/remove as needed.

Can somebody PLEASE help me sort this out. I'm trying to finish what should've been a simple program that I started making last year, and it's proving to be almost impossible for me to figure out.

I honestly don't know what the heck to do anymore. The documentation provides no useful information to me because none of it has ever worked. I've given sample code to others to use, and it works for them - but not me! How does that work?

I don't know if SENDING mail is dependent upon what security/firewall settings my computer has or not. But just in-case, I have gone so far as to completely turn off all security and firewall settings temporarily just to see if it would send an email.

I don't have code for it anymore since I have only just started trying to do this thing again and I would really appreciate it if somebody could assist me in getting this working.

So, all I am trying to do is:

Create a simple Form with 2 buttons and a textbox. (done, ofcourse) button1 checks for email (but only displays the subject and sender in a messagebox, does not download the message) button2 sends the contents of textBox1 to "[email protected]"

My server settings are:

Username    [email protected]
Password    ***********
IMAP/POP Server (Incoming):     mail.bluebottle.com
SMTP Server     (Outgoing):     mail.bluebottle.com

SMTP should be port 25, 26 or 587
POP3 should be port 110, using SSL 995
IMAP should be port 143, using SSL 993


Thanks for taking the time to read. If I haven't explained anything clearly please say so and I will try to make more sense out of it for you.

+2  A: 

Since this year Microsoft made pop3 and SMTP support available to all Hotmail users.

  • POP3 Server: pop3.live.com (port 995)
  • SMTP Server: smtp.live.com (port 25) {Note: If port 25 has been blocked in your network or by your ISP, you can set SMTP port to 587 with TLS or SSL Encryption depending on the client in use}

More info: http://windowslivehelp.com/solutions/settings/archive/2009/01/06/send-and-receive-windows-live-hotmail-emails-from-a-mail-client.aspx

ZippyV
thank you very much ZippyV
baeltazor
I don't think this is related to the question
Jonas Stawski
That's because the whole question has been changed in October.
ZippyV
+3  A: 

Never mind. I just figured it out for myself. It's a simple as 123! or is it ABC? I forget how it goes. Any how, incase anybody's interested or needs to know how to send e-mail in C#, this is what worked for ME:


string Sender     = "[email protected]";

string Username   = "username";
string Password   = "********";

string Recipient  = "[email protected]";

string Subject    = "Enter subject here.";
string Message    = "Enter message here.";

string Host       = "mail.server.com";
int Port          = 26;

MailMessage Mail = 
    new MailMessage(
    Sender,
    Recipient);

using(Mail)
{
        Mail.Subject     = Subject;
        Mail.Body        = Message;

        SmtpClient SmtpMail    =
        new SmtpClient(
        Host,
        Port);

        SmtpMail.EnableSsl     = true;

        SmtpMail.Credentials   =
        new System.Net.NetworkCredential(
        Username,
        Password);

        SmtpMail.Send(Mail);
}


Please note that the following using directive needs to be declared at the top of the document:

using System.Net.Mail;

Edit: Using Pattern docos: http://msdn.microsoft.com/en-us/library/yh598w02%28VS.71%29.aspx

baeltazor
Code sample is good, but please use the using pattern instead of manually disposing of the mail object.
Jesse Weigert
Thanks for the advice Jesse Weigert. May I ask, why do we need to use the using pattern instead of manually Disposing of the objects? Is it just personal preference? Performance related? Something else? I'm looking on MSDN now
baeltazor
you should take care of disposing even in case of an exception. This can be done by putting the dispose in a finally block, or - as syntactic sugar - doing the whole stuff inside a using block, which generates the same IL code as try { ... } finally { Mail.Dispose(); }
Marc Wittke
http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c
rohancragg
"It guarantees that Dispose will be called whatever happens... even if there is an exception thrown within the body of the using statement"
rohancragg
Thank you for your answers Rohancargg :)
baeltazor
And the cycle begins again... ;)
RCIX