Link to home
Start Free TrialLog in
Avatar of vynette10
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.mailmessage 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.ConnectSSLAuto;

            //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);
        }
Avatar of Smart_Man
Smart_Man
Flag of Egypt image

Avatar of vynette10
vynette10

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.GetTypeHashCode()': no suitable method found to override

3.'ASP.submitform_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override      
      
4.'ASP.submitform_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable'      

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.Async
{

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Net.Mail.MailMessage mailObj = new System.Net.Mail.MailMessage();

                //set user authentication
                mailObj.UserName = "LogonServerAccountName";
                mailObj.Password = "pleasehelp";

                //specifies the authentication mechanism.
                mailObj.AuthType = SmtpAuthType.AuthAuto;

                //set SSL connection
                mailObj.ConnectType = SmtpConnectType.ConnectSSLAuto;

                //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.com");
                Response.Write("Mail sent successfully");
            }
            catch (Exception x)
            {
                Response.Write("Your message was not sent: " + x.Message);
            }

        }
        protected void FirstNameTxt_TextChanged(object sender, EventArgs e)
        {

        }
        protected void LastNameTxt_TextChanged(object sender, EventArgs e)
        {

        }
        protected void TitleTxt_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
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 ?
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?
ASKER CERTIFIED SOLUTION
Avatar of Smart_Man
Smart_Man
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please delete the question. The answers were not helpful to me and I doubt they will be helpful to anyone else.