Re: Sending email

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
* sendRegistrationInfo, 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.Send(.... line throws an exception, your mailmenssage will never be disposed of.

3. You have try..catch..throw 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(System.Configuration.ConfigurationManager.AppSettings["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(From, To);
}

public class SendEmail
{
public static void sendEmail(Mail mail)
{
try
{
mail.client.Send(mail);
mail.msg.Dispose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}

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

public static void sendRegistrationInfo(string Recipient, string
ScreenName, string Password)
{
try
{
Mail mail = new Mail();
mail.From = new MailAddress("register@xxxxxxxx",
"register@xxxxxxxx");
mail.To = new MailAddress(Recipient);
mail.Subject += "User Registration";
mail.Body += Environment.NewLine;
mail.Body += Environment.NewLine + "Thank you for registering with
us! Here is your user information:";
mail.Body += Environment.NewLine + "Screen Name: " + ScreenName;
mail.Body += Environment.NewLine + "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
.



Relevant Pages

  • Re: RFC - New Object-Oriented Method of Parallel Programming
    ... programming methods precisely as means to prevent deadlocks. ... Having some experience with data-flow oriented languages like ... Labview like many similar packages are nothing but dataflow design fronts ... The exception issue is the simular thing - since Labview execution model is ...
    (comp.object)
  • Re: use of assert in Java [vs. exceptions]
    ... too categorical "Do not use assertions to check the parameters of a public method". ... The reasoning for my discrepancy towards that rule is that programs should not handle pre condition violations that are rooted in programming errors by means of exceptions e.g. IllegalArgumentException. ... exception was due to a floating-point error: a conversion from a 64-bit integer to a 16-bit signed integer, which should only have been applied to a number less than 2^15, was erroneously applied to a greater number, representing the "horizontal bias" of the flight. ...
    (comp.lang.java.programmer)
  • Re: Comparison with False - something I dont understand
    ... a single recently developed programming language that does not provide ... exception handling mechanisms because they have been proven more reliable. ...
    (comp.lang.python)
  • Re: Finally - what does it do?
    ... > all my many years of C++ programming. ... What happens if an exception is ... What if handleError throws an exception itself? ... >> So the variant with the finally block will still execute the tidyUp ...
    (microsoft.public.dotnet.languages.csharp)
  • How to get the value of an attribute in an XML node?
    ... of an xml file that I am trying to read and get the values of the ... XMLEventReader r = factory.createXMLEventReader; ... catch (Exception ex) ... public static void getGeneRecord{ ...
    (comp.lang.java.databases)