473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 **********@site .com", "do-not-
re***@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 ******@site.com ",
"re******@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
Nov 18 '07 #1
2 2675
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 **********@site .com", "do-not-
re***@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 ******@site.com ",
"re******@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
Nov 19 '07 #2

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" <fa**********@g mail.comwrote in message
news:96******** *************** ***********@o6g 2000hsd.googleg roups.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 **********@site .com", "do-not-
re***@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 ******@site.com ",
"re******@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

Nov 19 '07 #3

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

Similar topics

3
7034
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. Obviuosly, the file is fine on the server, so the attachment code I am using must be corrupting it, but I dont know what it is: // send email with...
0
522
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 import mimetypes import os,string
3
6814
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 (non-MyDomain.com address) I get the following error: The server rejected one or more recipient addresses. The server response was: 550 You must...
3
3605
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 smtpMail.Send("from@here.com", to@there.com, "Message subject", "Message Body") I'm sending it to my own email address on a different server using...
1
8168
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 the members of my organization. I think my problem is incorrectly setting the settings on my server or an authentication problem. Here is the code I...
2
12159
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, error is thrown at the .send point. Error is: The following error occured Sending an email: System.ApplicationException: An error occurred sending...
9
3448
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 oSMTP.Send(oMailMessage) (in this line I am getting the above err)
7
3324
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 guests added. form.php function createGuestNameAndEmailElements() {
10
5060
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> <zone>BOXING</zone> <status>Running</status> <job>1000139233</job>
31
12648
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 is working for normal email servers, but NOT with gmail server, I am very puzzled still!!) Codes(item c below) It keeps complaining and logs and...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.