Sending email

A

Allie

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.

[(...) 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("(e-mail address removed)", "do-not-
(e-mail address removed)");
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("(e-mail address removed)",
"(e-mail address removed)");
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
 
J

John B

Allie said:
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("(e-mail address removed)", "do-not-
(e-mail address removed)");
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("(e-mail address removed)",
"(e-mail address removed)");
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
 
S

sloan

I believe these 2 posts might help you.


http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!138.entry


http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx


Good luck.





Allie said:
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.

[(...) 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("(e-mail address removed)", "do-not-
(e-mail address removed)");
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("(e-mail address removed)",
"(e-mail address removed)");
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top