vynette10
asked on
Create C# webform for email submission using SMTP
Hello,
I'm a newbie and completely lost.
I am trying to create a webform in C# that will send an email to my email address with the code below. I need to send a username and password with the code so that the server connection can be made.
When I build the program, I am told that system.net.mail.mailmessag e doesn't have definitions for username, password, authorization, port, helodomain, mailfrom etc.
I think i'm missing a namespace (or more) but I'm having difficulty figuring out which one(s).
Please help me get my code working
Thanks
using System.Net.Mail;
try
{
MailMessage mailObj = new MailMessage();
//set user authentication
mailObj.UserName = "LogonServerName";
mailObj.Password = "pleasehelp";
//specifies the authentication mechanism.
mailObj.AuthType = SmtpAuthType.AuthAuto;
//set SSL connection
mailObj.ConnectType = SmtpConnectType.ConnectSSL Auto;
//set smtp server port (port 25 blocked by my isp so using alternate one)
mailObj.Port = ###;
//set helo domain
mailObj.HeloDomain = "myURL.com";
//set delivery-report address
mailObj.MailFrom = "you@myURL.com";
mailObj.From = "DoNotRespond@myURL.com";
mailObj.To = "me@myURL.com";
mailObj.Subject = "Submission Form";
mailObj.Body = "This is sample text";
mailObj.BodyFormat = MailFormat.Text;
mailObj.SmtpServer = "myURL.com";
mailObj.Send(mailObj);
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
I'm a newbie and completely lost.
I am trying to create a webform in C# that will send an email to my email address with the code below. I need to send a username and password with the code so that the server connection can be made.
When I build the program, I am told that system.net.mail.mailmessag
I think i'm missing a namespace (or more) but I'm having difficulty figuring out which one(s).
Please help me get my code working
Thanks
using System.Net.Mail;
try
{
MailMessage mailObj = new MailMessage();
//set user authentication
mailObj.UserName = "LogonServerName";
mailObj.Password = "pleasehelp";
//specifies the authentication mechanism.
mailObj.AuthType = SmtpAuthType.AuthAuto;
//set SSL connection
mailObj.ConnectType = SmtpConnectType.ConnectSSL
//set smtp server port (port 25 blocked by my isp so using alternate one)
mailObj.Port = ###;
//set helo domain
mailObj.HeloDomain = "myURL.com";
//set delivery-report address
mailObj.MailFrom = "you@myURL.com";
mailObj.From = "DoNotRespond@myURL.com";
mailObj.To = "me@myURL.com";
mailObj.Subject = "Submission Form";
mailObj.Body = "This is sample text";
mailObj.BodyFormat = MailFormat.Text;
mailObj.SmtpServer = "myURL.com";
mailObj.Send(mailObj);
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
ASKER
Thank you for your response. I am more clueless than I realized. I'm going to start posting the entire aspx.cs page.
I made the changes suggested above and now have new build errors. . .
1.Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). //*this causes Default2 to be underlined*
2.'ASP.submitform_aspx.Get TypeHashCo de()': no suitable method found to override
3.'ASP.submitform_aspx.Pro cessReques t(System.W eb.HttpCon text)': no suitable method found to override
4.'ASP.submitform_aspx' does not implement interface member 'System.Web.IHttpHandler.I sReusable'
Code is now:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Web.UI.HtmlControls ;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Asyn c
{
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(objec t sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.Net.Mail.MailMessag e mailObj = new System.Net.Mail.MailMessag e();
//set user authentication
mailObj.UserName = "LogonServerAccountName";
mailObj.Password = "pleasehelp";
//specifies the authentication mechanism.
mailObj.AuthType = SmtpAuthType.AuthAuto;
//set SSL connection
mailObj.ConnectType = SmtpConnectType.ConnectSSL Auto;
//set smtp server port (Port 25 blocked by my isp so using alternate one)
mailObj.Port = ###;
//set helo domain
mailObj.HeloDomain = "myURL.com";
//set delivery-report address
mailObj.MailFrom = "you@myURL.com";
mailObj.From = "me@myURL.com";
mailObj.To.Add = "DoNotRespond@myURL.com";
mailObj.Subject = " Submission Form";
mailObj.Body = "This is sample text";
mailObj.BodyFormat = MailFormat.Text;
mailObj.SmtpServer = "myURL.com";
mailObj.Send(mailObj);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient ("myURL.co m");
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
}
protected void FirstNameTxt_TextChanged(o bject sender, EventArgs e)
{
}
protected void LastNameTxt_TextChanged(ob ject sender, EventArgs e)
{
}
protected void TitleTxt_TextChanged(objec t sender, EventArgs e)
{
}
}
}
I made the changes suggested above and now have new build errors. . .
1.Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). //*this causes Default2 to be underlined*
2.'ASP.submitform_aspx.Get
3.'ASP.submitform_aspx.Pro
4.'ASP.submitform_aspx' does not implement interface member 'System.Web.IHttpHandler.I
Code is now:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Asyn
{
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(objec
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.Net.Mail.MailMessag
//set user authentication
mailObj.UserName = "LogonServerAccountName";
mailObj.Password = "pleasehelp";
//specifies the authentication mechanism.
mailObj.AuthType = SmtpAuthType.AuthAuto;
//set SSL connection
mailObj.ConnectType = SmtpConnectType.ConnectSSL
//set smtp server port (Port 25 blocked by my isp so using alternate one)
mailObj.Port = ###;
//set helo domain
mailObj.HeloDomain = "myURL.com";
//set delivery-report address
mailObj.MailFrom = "you@myURL.com";
mailObj.From = "me@myURL.com";
mailObj.To.Add = "DoNotRespond@myURL.com";
mailObj.Subject = " Submission Form";
mailObj.Body = "This is sample text";
mailObj.BodyFormat = MailFormat.Text;
mailObj.SmtpServer = "myURL.com";
mailObj.Send(mailObj);
System.Net.Mail.SmtpClient
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
}
protected void FirstNameTxt_TextChanged(o
{
}
protected void LastNameTxt_TextChanged(ob
{
}
protected void TitleTxt_TextChanged(objec
{
}
}
}
grrrrr .. i hate to go through code lines searching for errors. looool.
cannt we just talk about teh ideas ?! so the experince is on me and the work is on you ?? loooool
from the glance i took at the errors. you should use the autosense in teh IDE so you do not use somthign that is not there. make sure of the definition of teh calsses/liberaries/bla bla , whatever you are inheriting from above the lines you are going to call.
.... ummm...
what about having more in-depth look and get back ?
cannt we just talk about teh ideas ?! so the experince is on me and the work is on you ?? loooool
from the glance i took at the errors. you should use the autosense in teh IDE so you do not use somthign that is not there. make sure of the definition of teh calsses/liberaries/bla bla , whatever you are inheriting from above the lines you are going to call.
.... ummm...
what about having more in-depth look and get back ?
ASKER
I'm glad you said that. I'd love to talk about the concepts so I can learn more. I was afraid that if the discussion got too long or there were too many time gaps in the conversation, you would lose interest.
I'm having problems with the automatic code generated by Visual Studio. I have tried several ways to code this form. The previous ways I coded things created automatic code I need to change, but I don't know the right way to do it.
I have discovered commenting out this code leads to either more errors or new automatically generated code that recreates the same errors. Deletion of sections of code and deletion of the file the code is in also lead to more errors.
When I had this problem previously, I would create a new file and start over. I would leave the old file (and it's automatically generated code) in the project. However, I have finally gotten the page to look how I want it to look and am reluctant to start over for the 3rd time.
Any suggestions?
I'm having problems with the automatic code generated by Visual Studio. I have tried several ways to code this form. The previous ways I coded things created automatic code I need to change, but I don't know the right way to do it.
I have discovered commenting out this code leads to either more errors or new automatically generated code that recreates the same errors. Deletion of sections of code and deletion of the file the code is in also lead to more errors.
When I had this problem previously, I would create a new file and start over. I would leave the old file (and it's automatically generated code) in the project. However, I have finally gotten the page to look how I want it to look and am reluctant to start over for the 3rd time.
Any suggestions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please delete the question. The answers were not helpful to me and I doubt they will be helpful to anyone else.
http://www.devx.com/tips/Tip/29291
waiting for reply