Sending mail via SMTP server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iambenb

    Sending mail via SMTP server

    Hi all

    This is very frustrating. As far as I can tell my code should work.
    Indeed, I have got something similar working using VBA (and the
    CDO.Message object).

    I get the error "The operation has timed out" so I think that it's not
    connecting to the SMTP server at all. Anyone got ideas? Here's the
    code:

    <code>
    MailMessage m = new MailMessage();
    SmtpClient c = new SmtpClient();
    c.DeliveryMetho d = SmtpDeliveryMet hod.Network;

    // I've tried this with and without the domain
    System.Net.Netw orkCredential cred = new
    System.Net.Netw orkCredential(" SmtpUserName", "SmtpPassword") ;

    c.EnableSsl = true;
    c.UseDefaultCre dentials = false;
    c.Host = "SmtpServer ";
    c.Port = 465;
    c.Credentials = cred;

    m.To.Add(new MailAddress("so meone@mail.com" );
    m.From = new MailAddress("fr om@mail.com");
    m.Subject = "DotNet email";
    m.Body = "Hi Ben" + Environment.New Line + "This email
    was sent via code through SMTP";

    c.Send(m);
    </code>

    I can't see anything wrong with that. If anyone wants me to post the
    VBA code that does work, I will.
    I'm thinking that I may have to use CDO, which I don't really want to
    do.

    Thanks,
    Ben
  • =?Utf-8?B?QUEyZTcyRQ==?=

    #2
    RE: Sending mail via SMTP server

    I had problems with SMTP: mine were down to the virus checker blocking port
    25; this cause stray error to be reported.

    "iambenb" wrote:
    Hi all
    >
    This is very frustrating. As far as I can tell my code should work.
    Indeed, I have got something similar working using VBA (and the
    CDO.Message object).
    >
    I get the error "The operation has timed out" so I think that it's not
    connecting to the SMTP server at all. Anyone got ideas? Here's the
    code:
    >
    <code>
    MailMessage m = new MailMessage();
    SmtpClient c = new SmtpClient();
    c.DeliveryMetho d = SmtpDeliveryMet hod.Network;
    >
    // I've tried this with and without the domain
    System.Net.Netw orkCredential cred = new
    System.Net.Netw orkCredential(" SmtpUserName", "SmtpPassword") ;
    >
    c.EnableSsl = true;
    c.UseDefaultCre dentials = false;
    c.Host = "SmtpServer ";
    c.Port = 465;
    c.Credentials = cred;
    >
    m.To.Add(new MailAddress("so meone@mail.com" );
    m.From = new MailAddress("fr om@mail.com");
    m.Subject = "DotNet email";
    m.Body = "Hi Ben" + Environment.New Line + "This email
    was sent via code through SMTP";
    >
    c.Send(m);
    </code>
    >
    I can't see anything wrong with that. If anyone wants me to post the
    VBA code that does work, I will.
    I'm thinking that I may have to use CDO, which I don't really want to
    do.
    >
    Thanks,
    Ben
    >

    Comment

    • iambenb

      #3
      Re: Sending mail via SMTP server

      Thanks. I've definitely not got that problem, and in fact I can get
      this working using System.Web.Mail . Microsoft tells me that I should
      be using System.Net.Mail instead, but it doesn't work!!


      On 23 Oct, 10:56, AA2e72E <AA2e...@discus sions.microsoft .comwrote:
      I had problems with SMTP: mine were down to the virus checker blocking port
      25; this cause stray error to be reported.
      >
      "iambenb" wrote:
      Hi all
      >
      This is very frustrating. As far as I can tell my code should work.
      Indeed, I have got something similar working using VBA (and the
      CDO.Message object).
      >
      I get the error "The operation has timed out" so I think that it's not
      connecting to the SMTP server at all. Anyone got ideas? Here's the
      code:
      >
      <code>
                      MailMessage m = new MailMessage();
                      SmtpClient c = new SmtpClient();
                      c.DeliveryMetho d = SmtpDeliveryMet hod..Network;
      >
                      // I've tried this with and without thedomain
                      System.Net.Netw orkCredential cred = new
      System.Net.Netw orkCredential(" SmtpUserName", "SmtpPassword") ;
      >
                      c.EnableSsl = true;
                      c.UseDefaultCre dentials = false;
                      c.Host = "SmtpServer ";
                      c.Port = 465;
                      c.Credentials = cred;
      >
                      m.To.Add(new MailAddress("so me...@mail.com" );
                      m.From = new MailAddress("f. ..@mail.com");
                      m.Subject = "DotNet email";
                      m.Body = "Hi Ben" + Environment.New Line + "This email
      was sent via code through SMTP";
      >
                      c.Send(m);
      </code>
      >
      I can't see anything wrong with that. If anyone wants me to post the
      VBA code that does work, I will.
      I'm thinking that I may have to use CDO, which I don't really want to
      do.
      >
      Thanks,
      Ben

      Comment

      • =?ISO-8859-1?Q?Andreas_Kleib=F6hmer?=

        #4
        Re: Sending mail via SMTP server

        iambenb schrieb:
        I can't see anything wrong with that.
        Works for me with the same construction (other variable values, of
        course, for server, username etc..)

        But I did not see you supplied a "sender". If I leave that out, our
        mailserver fortunately complains.

        Does it work for you, if you also supply a sender to the message?

        Comment

        • sloan

          #5
          Re: Sending mail via SMTP server


          http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!138.entry
          Try this....which allows "tweaking" in a much easier way.

          There is a note about port 465 in the downloadable zip as well.




          "iambenb" <Ben.Bawden@gma il.comwrote in message
          news:09d0b3c6-4f64-4726-a1b0-894c7583b7bc@m7 4g2000hsh.googl egroups.com...
          Hi all
          >
          This is very frustrating. As far as I can tell my code should work.
          Indeed, I have got something similar working using VBA (and the
          CDO.Message object).
          >
          I get the error "The operation has timed out" so I think that it's not
          connecting to the SMTP server at all. Anyone got ideas? Here's the
          code:
          >
          <code>
          MailMessage m = new MailMessage();
          SmtpClient c = new SmtpClient();
          c.DeliveryMetho d = SmtpDeliveryMet hod.Network;
          >
          // I've tried this with and without the domain
          System.Net.Netw orkCredential cred = new
          System.Net.Netw orkCredential(" SmtpUserName", "SmtpPassword") ;
          >
          c.EnableSsl = true;
          c.UseDefaultCre dentials = false;
          c.Host = "SmtpServer ";
          c.Port = 465;
          c.Credentials = cred;
          >
          m.To.Add(new MailAddress("so meone@mail.com" );
          m.From = new MailAddress("fr om@mail.com");
          m.Subject = "DotNet email";
          m.Body = "Hi Ben" + Environment.New Line + "This email
          was sent via code through SMTP";
          >
          c.Send(m);
          </code>
          >
          I can't see anything wrong with that. If anyone wants me to post the
          VBA code that does work, I will.
          I'm thinking that I may have to use CDO, which I don't really want to
          do.
          >
          Thanks,
          Ben

          Comment

          Working...