Sending email

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

    #1

    Sending email

    Hi, all.

    I'm new to C# programming. I'm even newer to programming email
    capabilities into my code. What I need to do is create three (3)
    functions:
    * sendEmail, using a Mail object (which needs to be created) as a
    parameter
    * forgotPassword, using whatever arguments it needs -- uses sendEmail
    to send lost password to user
    * sendRegistratio nInfo, using whatever arguments it needs -- uses
    sendEmail to send registration info to user

    Can someone please take a look at this code and tell me if it makes
    sense? Building the code doesn't throw any errors.

    [(...) denotes code that has been eliminated because it is not
    pertinent to the code at hand.]

    =============== =============== ============

    (...)
    using System.Net;
    using System.Net.Mail ;
    using System.Net.Mime ;
    (...)

    (...)

    public class Mail
    {
    SmtpClient client = new
    SmtpClient(Syst em.Configuratio n.Configuration Manager.AppSett ings["Smtp"].ToString());
    MailAddress From;
    MailAddress To;
    String Subject = "[Company Name] "; // rest of subject will be
    appended as necessary in appropriate function(s)
    String Body = "Hi,"; // rest of message will be appended as necessary
    in appropriate function(s)
    System.Net.Mail .MailMessage msg = new
    System.Net.Mail .MailMessage(Fr om, To);
    }

    public class SendEmail
    {
    public static void sendEmail(Mail mail)
    {
    try
    {
    mail.client.Sen d(mail);
    mail.msg.Dispos e();
    }
    catch (Exception e)
    {
    throw new Exception(e.Mes sage);
    }
    }
    }

    public static void forgotPassword( string Recipient, string Password)
    {
    try
    {
    Mail mail = new Mail();
    mail.From = new MailAddress("do-not-reply@site.com" , "do-not-
    reply@site.com");
    mail.To = new MailAddress(Rec ipient);
    mail.Subject += "Your Password";
    mail.Body += Environment.New Line;
    mail.Body += Environment.New Line + "You requested recovery of your
    password. Here is your password:";
    mail.Body += Environment.New Line + Password;
    sendEmail(mail) ;
    }
    catch (Exception e)
    {
    throw e;
    }
    }

    public static void sendRegistratio nInfo(string Recipient, string
    ScreenName, string Password)
    {
    try
    {
    Mail mail = new Mail();
    mail.From = new MailAddress("re gister@site.com ",
    "register@site. com");
    mail.To = new MailAddress(Rec ipient);
    mail.Subject += "User Registration";
    mail.Body += Environment.New Line;
    mail.Body += Environment.New Line + "Thank you for registering with
    us! Here is your user information:";
    mail.Body += Environment.New Line + "Screen Name: " + ScreenName;
    mail.Body += Environment.New Line + "Password: " + Password;
    sendEmail(mail) ;
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    }

    =============== =============== ============

    Again, I'm new to all of this. Also. My professors have always told me
    that my logic is wonky. So when errors do pop up, I'm usually confused
    because my logic flow makes sense to me. The compiler, however, seems
    to think otherwise :P

    I would really appreciate any criticism (constructive or otherwise).

    Thanks,
    Allie
  • John B

    #2
    Re: Sending email

    Allie wrote:
    Hi, all.
    >
    I'm new to C# programming. I'm even newer to programming email
    capabilities into my code. What I need to do is create three (3)
    functions:
    * sendEmail, using a Mail object (which needs to be created) as a
    parameter
    * forgotPassword, using whatever arguments it needs -- uses sendEmail
    to send lost password to user
    * sendRegistratio nInfo, using whatever arguments it needs -- uses
    sendEmail to send registration info to user
    >
    Can someone please take a look at this code and tell me if it makes
    sense? Building the code doesn't throw any errors.
    >
    <...>

    Have you run the code? Does it work?

    A couple of things:

    1. Please do not store passwords. Rather store a hash of the password
    and provide a password reset email instead. Google for password hash if
    you don't understand.

    2. It is bad form (IMO) to dispose of a passed object, rather declare it
    in a using block or try-finally. If your mail.client.Sen d(.... line
    throws an exception, your mailmenssage will never be disposed of.

    3. You have try..catch..thr ow which appears to be doing nothing. Is this
    just for debugging?

    4. Please dont wrap every method in empty try/catch blocks, this just
    clutters up your code.

    A question like yours is really hard for the group to answer, rather
    than asking general questions such as these, get your program working
    and ask a question when you get stuck or ask a specific style related
    question.

    HTH

    JB
    [(...) denotes code that has been eliminated because it is not
    pertinent to the code at hand.]
    >
    =============== =============== ============
    >
    (...)
    using System.Net;
    using System.Net.Mail ;
    using System.Net.Mime ;
    (...)
    >
    (...)
    >
    public class Mail
    {
    SmtpClient client = new
    SmtpClient(Syst em.Configuratio n.Configuration Manager.AppSett ings["Smtp"].ToString());
    MailAddress From;
    MailAddress To;
    String Subject = "[Company Name] "; // rest of subject will be
    appended as necessary in appropriate function(s)
    String Body = "Hi,"; // rest of message will be appended as necessary
    in appropriate function(s)
    System.Net.Mail .MailMessage msg = new
    System.Net.Mail .MailMessage(Fr om, To);
    }
    >
    public class SendEmail
    {
    public static void sendEmail(Mail mail)
    {
    try
    {
    mail.client.Sen d(mail);
    mail.msg.Dispos e();
    }
    catch (Exception e)
    {
    throw new Exception(e.Mes sage);
    }
    }
    }
    >
    public static void forgotPassword( string Recipient, string Password)
    {
    try
    {
    Mail mail = new Mail();
    mail.From = new MailAddress("do-not-reply@site.com" , "do-not-
    reply@site.com");
    mail.To = new MailAddress(Rec ipient);
    mail.Subject += "Your Password";
    mail.Body += Environment.New Line;
    mail.Body += Environment.New Line + "You requested recovery of your
    password. Here is your password:";
    mail.Body += Environment.New Line + Password;
    sendEmail(mail) ;
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    >
    public static void sendRegistratio nInfo(string Recipient, string
    ScreenName, string Password)
    {
    try
    {
    Mail mail = new Mail();
    mail.From = new MailAddress("re gister@site.com ",
    "register@site. com");
    mail.To = new MailAddress(Rec ipient);
    mail.Subject += "User Registration";
    mail.Body += Environment.New Line;
    mail.Body += Environment.New Line + "Thank you for registering with
    us! Here is your user information:";
    mail.Body += Environment.New Line + "Screen Name: " + ScreenName;
    mail.Body += Environment.New Line + "Password: " + Password;
    sendEmail(mail) ;
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    }
    >
    =============== =============== ============
    >
    Again, I'm new to all of this. Also. My professors have always told me
    that my logic is wonky. So when errors do pop up, I'm usually confused
    because my logic flow makes sense to me. The compiler, however, seems
    to think otherwise :P
    >
    I would really appreciate any criticism (constructive or otherwise).
    >
    Thanks,
    Allie

    Comment

    • sloan

      #3
      Re: Sending email


      I believe these 2 posts might help you.


      http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!138.entry


      http://blogs.msdn.com/kcwalina/archi...16/396787.aspx


      Good luck.





      "Allie" <fakeprogress@g mail.comwrote in message
      news:96e39ae3-fece-42ce-bd25-30655de4eeec@o6 g2000hsd.google groups.com...
      Hi, all.
      >
      I'm new to C# programming. I'm even newer to programming email
      capabilities into my code. What I need to do is create three (3)
      functions:
      * sendEmail, using a Mail object (which needs to be created) as a
      parameter
      * forgotPassword, using whatever arguments it needs -- uses sendEmail
      to send lost password to user
      * sendRegistratio nInfo, using whatever arguments it needs -- uses
      sendEmail to send registration info to user
      >
      Can someone please take a look at this code and tell me if it makes
      sense? Building the code doesn't throw any errors.
      >
      [(...) denotes code that has been eliminated because it is not
      pertinent to the code at hand.]
      >
      =============== =============== ============
      >
      (...)
      using System.Net;
      using System.Net.Mail ;
      using System.Net.Mime ;
      (...)
      >
      (...)
      >
      public class Mail
      {
      SmtpClient client = new
      SmtpClient(Syst em.Configuratio n.Configuration Manager.AppSett ings["Smtp"].ToString());
      MailAddress From;
      MailAddress To;
      String Subject = "[Company Name] "; // rest of subject will be
      appended as necessary in appropriate function(s)
      String Body = "Hi,"; // rest of message will be appended as necessary
      in appropriate function(s)
      System.Net.Mail .MailMessage msg = new
      System.Net.Mail .MailMessage(Fr om, To);
      }
      >
      public class SendEmail
      {
      public static void sendEmail(Mail mail)
      {
      try
      {
      mail.client.Sen d(mail);
      mail.msg.Dispos e();
      }
      catch (Exception e)
      {
      throw new Exception(e.Mes sage);
      }
      }
      }
      >
      public static void forgotPassword( string Recipient, string Password)
      {
      try
      {
      Mail mail = new Mail();
      mail.From = new MailAddress("do-not-reply@site.com" , "do-not-
      reply@site.com");
      mail.To = new MailAddress(Rec ipient);
      mail.Subject += "Your Password";
      mail.Body += Environment.New Line;
      mail.Body += Environment.New Line + "You requested recovery of your
      password. Here is your password:";
      mail.Body += Environment.New Line + Password;
      sendEmail(mail) ;
      }
      catch (Exception e)
      {
      throw e;
      }
      }
      >
      public static void sendRegistratio nInfo(string Recipient, string
      ScreenName, string Password)
      {
      try
      {
      Mail mail = new Mail();
      mail.From = new MailAddress("re gister@site.com ",
      "register@site. com");
      mail.To = new MailAddress(Rec ipient);
      mail.Subject += "User Registration";
      mail.Body += Environment.New Line;
      mail.Body += Environment.New Line + "Thank you for registering with
      us! Here is your user information:";
      mail.Body += Environment.New Line + "Screen Name: " + ScreenName;
      mail.Body += Environment.New Line + "Password: " + Password;
      sendEmail(mail) ;
      }
      catch (Exception e)
      {
      throw e;
      }
      }
      }
      >
      =============== =============== ============
      >
      Again, I'm new to all of this. Also. My professors have always told me
      that my logic is wonky. So when errors do pop up, I'm usually confused
      because my logic flow makes sense to me. The compiler, however, seems
      to think otherwise :P
      >
      I would really appreciate any criticism (constructive or otherwise).
      >
      Thanks,
      Allie

      Comment

      Working...