tags:

views:

424

answers:

4

The following code nets me out error after error.

        MailMessage Enveloppe = new MailMessage();

        //emails is an array of strings
        foreach ( string email in emails )
            Enveloppe.To.Add(email);

        //Setup message parameters
        Enveloppe.Subject = "Documentation";
        Enveloppe.Body = "Infinitelycoolbodyhere"
        Enveloppe.From = new MailAddress("mrzombie@stuff", "Myname");

        //files is an array of strings
        foreach ( string filename in files ) { 
            //Add attachments
            Enveloppe.Attachments.Add(new Attachment(new System.Net.WebClient().OpenRead(filename),filename.Substring(filename.LastIndexOf('/'))));
        }

        //Create the smtp client, and let it do its job
        SmtpClient Mailman = new SmtpClient("mysmtphost");
        Mailman.EnableSsl = true;
        Mailman.Port = 465;
        Mailman.DeliveryMethod = SmtpDeliveryMethod.Network;
        Mailman.Credentials = new System.Net.NetworkCredential("usernamehere", "passwordhere");
        Mailman.Send(Enveloppe);

It so happens that it either tells me that my operation timed out, or that "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond".

I'm stumped, I don't see which part of it I am doing wrong

Update: I can't seem to connect to the server from Telnet, at best I connect with my laptop (under ubuntu), but I never get the "200" response. Under the windows workstation, I get a perpetual blank telnet window.

I can send/receive e-mail at the specified server with Thunderbird without any problem whatsoever.

A: 

Is that SMTP server up and running on 465 and does it support SSL?

JoshBerke
I tried two or three things when a first server failed to respond.I tried using my personnal website's smtp server using ssl, hence the ssl part in the code. I didn't succeed, the operation timed out, but since I'm able to send-receive e-mail with the server, using encryption, I'd have to assume that it is up and running.
MrZombie
A: 

Sounds like something went wrong when trying to communicate with your SMTP server(hah, no shit!).

I assume you're on windows so go to Start -> Run -> 'telnet mysmtphost 465' and see the response you get in the console.

Do you see something like this?

Connected to mysmtphost.
Escape character is '^]'.
220 mysmtphost
Ghazaly
From my Ubuntu laptop I get the expected result. From my workstation on the same network (the thingie executing the code), I get an eternally blank telnet window.
MrZombie
Oh, I don't have the "200 mysmtphost" in either case.
MrZombie
Guess: If my laptop is able to send/receive emails, then I should check that the machine I work on and the one that's going to run the program don't have any firewall rule for port 465 or 25, right?
MrZombie
Don't worry about the eternally blank telnet window. It means that you can connect to the smtp server perfectly fine. Please try to telnet from machine that you are working on. If you can't connect to the smtp server, then I think its reasonable to assume that its a firewall issue.
Ghazaly
A: 

Hi,

Try enabling tracing for System.Net.Mail (looks like you are using SNM). To do this, you will want to add the following to your .config file.

<sources>

  <source name="System.Net" >
    <listeners>
      <add name="MyTraceFile"/>
    </listeners>
  </source>

  <source name="System.Net.Sockets">
    <listeners>
      <add name="MyTraceFile"/>
    </listeners>
  </source>

</sources>


<sharedListeners>
  <add
    name="MyTraceFile"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="System.Net.trace.log"                />
</sharedListeners>

<switches>
  <add name="System.Net" value="Verbose" />
  <add name="System.Net.Sockets" value="Verbose" />
</switches>

Hopefully this should at least tell you what is happening.

I've got some more info here, on what to expect: http://systemnetmail.com/faq/4.10.aspx

Cheers!

Dave

dave wanta
A: 

Finally found the culprit - somehow the connection was left hanging between the "supposedly correct" smtp server the hosting company was giving me, and the actual smtp server. I just changed the address of mail.domain.com to smtp.hostingdomain.com and it fixed everything.

This is just one of those "what the hell" moments that I hate. Because of it, I had to drop the feature of application-based mail-sending, but in the end my boss and I decided that we'd instead host the files somewhere and include the links in the e-mail instead of appending attached files to the mail.

So, yeah, that doesn't really answer the question, but to those who consider the solution, but you might want to put the files you want to send on a place accessible from the outside and include linkage instead. There.

MrZombie