SMTP Mail .Net 2.0
Sending Email with ASP.Net 2.0 C#
At Aquest Hosting, we require mail to be authenticated to go through our SMTP server, but this function is setup to take a parameter specifying whether or not smtp authentication should occur. So if you are an Aquest Hosting customer, you should use 'true' for that parameter, otherwise consult your system admin or web host to determine if you should use authentication.
Note that this code is in a code behind page with the appropriate text boxes in the first function to get data from the user. I'm leaving it up to you to create the page and or collect your data. The main point here is the 2nd function that actually is responsible for sending the email.
Here is the sample code in ASP.NET 2.0 C#:
protected void cmdSend_Click( object sender, EventArgs e)
{
string serv = this .txtServer.Text.Trim();
string uname = this .txtUserName.Text.Trim();
string pw = this .txtPassword.Text.Trim();
string to = this .txtTo.Text.Trim();
string from = this .txtFrom.Text.Trim();
string subj = this .txtSubj.Text.Trim();
string body = this .txtBody.Text.Trim();
bool auth = this .ckUseAuth.Checked;
SendSmtpMail(serv, auth, uname, pw, to, from, subj, body);
}
privatevoid SendSmtpMail( string SmtpServer, bool UseAuth, string UName, string PW, string To, string From, string Subject, string Body )
{
try
{
System.Net.Mail. SmtpClient smtp = new System.Net.Mail. SmtpClient (SmtpServer);
System.Net.Mail. MailMessage mssg = new System.Net.Mail. MailMessage (From, To, Subject, Body);
mssg.IsBodyHtml = true ;
if (UseAuth)
smtp.Credentials = new System.Net. NetworkCredential (UName, PW);
smtp.Send(mssg);
this .litError.Text = "No Errors when sending." ;
}
catch ( Exception ex)
{
this .litError.Text = ex.Message.Trim();
}
}