Send email form C#

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

    Send email form C#

    Hello All,

    I wrote the follownig program

    Code:
    using System;
    using System.Web.Mail;
    public class MailHelper
    {
    public static void SendMail(string mailId, string author, string email,
    string title, string msgDate, string comment)
    {
    String msg = String.Format("{0} (email: {1}) has just posted a comment
    on the message \"{2}\" of your BLOG that you posted at {3}. Here's the
    comment: {4}{4}{5}",
    author,
    email,
    title,
    msgDate,
    Environment.NewLine,
    comment
    );
    try
    {
    //SmtpMail.SmtpServer = "localhost";
    MailMessage mail = new MailMessage();
    mail.To = mailId;
    mail.From = "abhishes@hotmail.com";
    mail.Subject = "New BLOG comment notification";
    mail.Body = msg;
    SmtpMail.Send(mail);
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex);
    Console.WriteLine("*****************");
    Console.WriteLine(ex.InnerException.Message);
    Console.WriteLine("*****************");
    Console.WriteLine(ex.InnerException.InnerException.Message);
    throw ex;
    }
    }
    }
    The code doesn't throw any exception ... but it doesn't send the mail as
    well.

    I have configured the SMTP service on my local machine itself. I have
    also gone into SMTP server properties and into the "Access->Relay"
    option and added my machine IP (127.0.0.1 and my ip address) into the
    list of "only the list below" option.

    Please do help me... why am I not able to send the email?

    regards,
    Abhishek.

  • Patrick Schwegler

    #2
    Re: Send email form C#

    Abhishek Srivastava wrote:
    [cut][color=blue]
    >
    > Please do help me... why am I not able to send the email?
    >
    > regards,
    > Abhishek.
    >[/color]

    Hi,
    Try to uncomment
    Code:
    SmtpMail.SmtpServer = "localhost";

    Greets
    Patrick


    Comment

    • Abhishek Srivastava

      #3
      Re: Send email form C#

      If I do that I get the exception

      System.Web.Http Exception: Could not access 'CDO.Message' object. --->
      System.Reflecti on.TargetInvoca tionException: Exception has been thrown
      by the target of an invocation. --->
      System.Runtime. InteropServices .COMException (0x80040213): The transport
      failed to connect to the server.

      --- End of inner exception stack trace ---
      at System.RuntimeT ype.InvokeDispM ethod(String name, BindingFlags
      invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
      Int32 culture, String[] namedParameters )
      at System.RuntimeT ype.InvokeMembe r(String name, BindingFlags
      invokeAttr, Binder binder, Object target, Object[] args,
      ParameterModifi er[] modifiers, CultureInfo culture, String[]
      namedParameters )
      at System.Type.Inv okeMember(Strin g name, BindingFlags invokeAttr,
      Binder binder, Object target, Object[] args)
      at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Type type,
      Object obj, String methodName, Object[] args)
      at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Object obj,
      String methodName, Object[] args)
      --- End of inner exception stack trace ---
      at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Object obj,
      String methodName, Object[] args)
      at System.Web.Mail .CdoSysHelper.S end(MailMessage message)
      at System.Web.Mail .SmtpMail.Send( MailMessage message)
      at BLogComponents. MailHelper.Send Mail(String mailId, String author,
      String email, String title, String msgDate, String comment) in
      c:\myapps\dotne t\blog\componen ts\mailhelper.c s:line 25
      *************** **
      Exception has been thrown by the target of an invocation.
      *************** **
      The transport failed to connect to the server.


      Patrick Schwegler wrote:[color=blue]
      > Abhishek Srivastava wrote:
      > [cut]
      >[color=green]
      >>
      >> Please do help me... why am I not able to send the email?
      >>
      >> regards,
      >> Abhishek.
      >>[/color]
      >
      > Hi,
      > Try to uncomment
      >
      Code:
      >     SmtpMail.SmtpServer = "localhost";
      >
      >
      >
      > Greets
      > Patrick
      >
      >[/color]

      Comment

      • Girish Bharadwaj

        #4
        Re: Send email form C#

        Abhishek Srivastava wrote:
        [color=blue]
        > If I do that I get the exception
        >
        > System.Web.Http Exception: Could not access 'CDO.Message' object. --->
        > System.Reflecti on.TargetInvoca tionException: Exception has been thrown
        > by the target of an invocation. --->
        > System.Runtime. InteropServices .COMException (0x80040213): The transport
        > failed to connect to the server.
        >
        > --- End of inner exception stack trace ---
        > at System.RuntimeT ype.InvokeDispM ethod(String name, BindingFlags
        > invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
        > Int32 culture, String[] namedParameters )
        > at System.RuntimeT ype.InvokeMembe r(String name, BindingFlags
        > invokeAttr, Binder binder, Object target, Object[] args,
        > ParameterModifi er[] modifiers, CultureInfo culture, String[]
        > namedParameters )
        > at System.Type.Inv okeMember(Strin g name, BindingFlags invokeAttr,
        > Binder binder, Object target, Object[] args)
        > at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Type type, Object
        > obj, String methodName, Object[] args)
        > at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Object obj,
        > String methodName, Object[] args)
        > --- End of inner exception stack trace ---
        > at System.Web.Mail .LateBoundAcces sHelper.CallMet hod(Object obj,
        > String methodName, Object[] args)
        > at System.Web.Mail .CdoSysHelper.S end(MailMessage message)
        > at System.Web.Mail .SmtpMail.Send( MailMessage message)
        > at BLogComponents. MailHelper.Send Mail(String mailId, String author,
        > String email, String title, String msgDate, String comment) in
        > c:\myapps\dotne t\blog\componen ts\mailhelper.c s:line 25
        > *************** **
        > Exception has been thrown by the target of an invocation.
        > *************** **
        > The transport failed to connect to the server.
        >
        >
        > Patrick Schwegler wrote:
        >[color=green]
        >> Abhishek Srivastava wrote:
        >> [cut]
        >>[color=darkred]
        >>>
        >>> Please do help me... why am I not able to send the email?
        >>>
        >>> regards,
        >>> Abhishek.
        >>>[/color]
        >>
        >> Hi,
        >> Try to uncomment
        >>
        Code:
        >>     SmtpMail.SmtpServer = "localhost";
        >>
        >>
        >>
        >> Greets
        >> Patrick
        >>
        >>[/color]
        >[/color]

        It almost sounds like your SMTP service is either not running or not
        accepting calls. Try going to Outlook Express and set the SMTP server to
        be localhost and try sending an email. Also, check the BadMail Folder
        under mailroot directory. You might get some insight.

        --
        Girish Bharadwaj

        Comment

        • Cybertof

          #5
          Re: Send email form C# (CDO is the only way ?)

          Is it the only to send an Email using CDO ?
          In the past, CDO was meaning of having at least a ms-exchange client on
          the pc (like outlook), and also a correct profile.

          Is there another way to send an email more directly ? a SMTP Class ?

          Regards,
          Cybertof.


          In article <OQh0hvxkDHA.24 88@TK2MSFTNGP12 .phx.gbl>, abhishek@nospam .net
          says...[color=blue]
          > If I do that I get the exception
          >
          > System.Web.Http Exception: Could not access 'CDO.Message' object. --->
          > System.Reflecti on.TargetInvoca tionException: Exception has been thrown
          > by the target of an invocation. --->[/color]

          Comment

          • Steven

            #6
            Re: Send email form C# (CDO is the only way ?)

            You will need outlook or outlook express to use the SMTPMail Class as it
            utilizies the CDO object.

            If you don't want to rely on CDO, you will need to write a class that can
            connect to an SMTP server and execute the neccessary commands.


            "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
            news:MPG.19f791 1e56f7369e9896b 2@msnews.micros oft.com...[color=blue]
            > Is it the only to send an Email using CDO ?
            > In the past, CDO was meaning of having at least a ms-exchange client on
            > the pc (like outlook), and also a correct profile.
            >
            > Is there another way to send an email more directly ? a SMTP Class ?
            >
            > Regards,
            > Cybertof.
            >
            >
            > In article <OQh0hvxkDHA.24 88@TK2MSFTNGP12 .phx.gbl>, abhishek@nospam .net
            > says...[color=green]
            > > If I do that I get the exception
            > >
            > > System.Web.Http Exception: Could not access 'CDO.Message' object. --->
            > > System.Reflecti on.TargetInvoca tionException: Exception has been thrown
            > > by the target of an invocation. --->[/color][/color]


            Comment

            Working...