473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
* 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.ConfigurationManag er.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**********@site.com", "do-not-
re***@site.com");
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("re******@site.com",
"re******@site.com");
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
Nov 18 '07 #1
2 2655
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.ConfigurationManag er.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**********@site.com", "do-not-
re***@site.com");
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("re******@site.com",
"re******@site.com");
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
Nov 19 '07 #2

I believe these 2 posts might help you.
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!138.entry
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
Good luck.

"Allie" <fa**********@gmail.comwrote in message
news:96**********************************@o6g2000h sd.googlegroups.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
* 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.ConfigurationManag er.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**********@site.com", "do-not-
re***@site.com");
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("re******@site.com",
"re******@site.com");
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

Nov 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
0
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils...
3
by: VB Programmer | last post by:
I have an ASPX page where I send out emails through my mail server mail.MyDomain.com. When I send emails to MyName@MyDomain.com it sends PERFECTLY. When I try sending an email to any other address...
3
by: Ant | last post by:
Hi, I'm using the MailMessage & smtpMail classes in System.Web.Mail to send mail, however it's not sending any emails. I'm using it on a Windows 2003 server. The simplest way to use this is...
1
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a web site I have created to...
2
by: =?Utf-8?B?QWRl?= | last post by:
HI All, I am encountering the following error when I try to send an email through a SMTP server. I believe the problem lies with the authentication part when the network crednetials are used,...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
7
by: bleachie | last post by:
Hey, I just need some help, my form seems to not send me all of the 'guestNames' and 'guestEmails' forms. i use this function to add more guestnames and guestemail fields based on the number of...
10
by: Markgoldin | last post by:
I am sending an XML data from not dontnet process to a .Net via socket listener. Here is a data sample: <VFPData> <serverdata> <coderun>updateFloor</coderun> <area>MD2</area>...
31
by: happyse27 | last post by:
Hi All, I am trying for weeks how to send email from windows pc, which from my gmail account to my hotmail account. Using net::smtp module sending email failed,Kindly assist. (for the item d it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.