0

Hello guys,
I have a simple asp.net form and ideally, upon submission, I'd like to forward the form data to an email address. Here is the form:

<div class="contactForm">
    <h2>Contact me</h2>
    <div class="form">   
        <div class="control-group">        
            <label class="control-label" for="title">Title</label>
            <div class="controls">
                <select id="title" runat="server">
                    <option>Mr</option>
                    <option>Ms</option>
                    <option>Miss</option>
                    <option>Mrs</option>
                </select>
            </div>
        </div>
        <div class="control-group">        
            <label class="control-label" for="name">Name</label>
            <div class="controls">
                <input runat="server" type="text" id="name" />
            </div>
        </div>
         <div class="control-group">        
            <label class="control-label" for="email">Email</label>
            <div class="controls">
                <input id="email" runat="server" type="text" />
            </div>
        </div>
         <div class="control-group">        
            <label class="control-label" for="enquiry">Enquiry</label>
            <div class="controls">
                <textarea id="enquiry" runat="server" type="text"></textarea>
            </div>
        </div>
        <div class="submitButton">
            <input id="submitForm" type="submit" value="Submit" runat="server" onserverclick="submitData">
            <div class="clear"></div>
        </div>

    </div>
</div>

Now, I had a look around the net, and found an interesting tutorial - not tested - http://www.mikesdotnetting.com/article/41/send-form-content-by-email-in-asp-net which I was planning to try. There is one thing though, he uses this script:

using (MailMessage message = new MailMessage())
{  
  message.From = new MailAddress(YourEmail.Text.ToString());  
  message.To.Add(new MailAddress("me@domain.com"));  
  message.CC.Add(new MailAddress("copy@domain.com"));  
  message.Subject = "Message via My Site from " + YourName.Text.ToString();  
  message.Body = Comments.Text.ToString();  
  SmtpClient client = new SmtpClient();  
  client.Host = "127.0.0.1";  
  client.Send(message);    
}

and I'm not sure where to get the values for the below, as I'm developing from localhost

message.To.Add(new MailAddress(""));  
message.CC.Add(new MailAddress(""));  
client.Host = "";

Any idea?
EDIT: I mean, does is make any difference what email provider I'm trying to forward the email to, or is it irrelevant? I'd like it to go to a gmail address

Edited by Violet_82

2
Contributors
10
Replies
33
Views
3 Years
Discussion Span
Last Post by Violet_82
0

I mean, does is make any difference what email provider I'm trying to forward the email to, or is it irrelevant? I'd like it to go to a gmail address

Yes it matters. Your email provider may or may not allow you to send email, unauthenticated on port 25. Some providers may require you to authenticate and use a different port.

message.To.Add(new MailAddress(""));

Email address of who you are sending to.

message.CC.Add(new MailAddress(""));

email address if you are going to carbon copy someone.

client.Host = "";

IP address or DNS host name of the smtp server

I dont know if your code is 100% correct, although a quick look doesnt have anything jumping out at me as a problem.

the MailMessage class has alot of options so you may want to do a search on MSDN to get a full list of properties and methods that are available to you.

Edited by JorgeM

0

Hi, OK I played around with that a little and eventually I had to change the code and try to send emails through gmail as I don't have access to any other smtp server. So, this is the code that I came up with.
About.aspx:

 <div class="submitButton">
                <input id="submitForm" type="submit" value="Submit" runat="server" onserverclick="submitData">
                <div class="clear"></div>
            </div>

About.aspx.cs

using System.Net.Mail;
...
    protected void submitData(object sender, EventArgs e) {
    //send email
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 25);
    smtp.Credentials = new System.Net.NetworkCredential("bassabasso@gmail.com", "xxxxxxxx");
    smtp.UseDefaultCredentials = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.EnableSsl = true;
    MailMessage mail = new MailMessage();
    mail.To.Add(new MailAddress("bassabasso@gmail.com"));
    mail.From = new MailAddress("bassabasso@gmail.com");
    mail.Subject = "test";
    mail.Body = "Hello this is a test";

    //smtp.Host = "smtp.gmail.com";


    smtp.Send(mail);
    //end of send email  
    }

So, following your suggestions, I've specified port 25 but the sender and the recipient address coincides, I hope that's not a problem. I had a very good look on the net, the problem is that everybody seems to be doing it differently, so for me it is quite difficult to judge which one is right and which one is wrong. The above code compiles, but when I press the submit button I get an error and I noticed an interesting fact as well. SO, with order, here is the error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Stack Trace:

[SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at]
   System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) +1061287
   System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) +41
   System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) +101
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1480
   About.submitData(Object sender, EventArgs e) +235
   System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) +111
   System.Web.UI.HtmlControls.HtmlInputButton.RaisePostBackEvent(String eventArgument) +109
   System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

The interesting fact was that at the email address specified in the code, I received an email saying that somebody had attempted to sign in and bla bla, so went into gmail settings and tick the "it was me" trying to sign in from there. Since I was there I also enabled POP and IMAP just in case. So I tried submitting the form again, I didn't receive any email in my gmail and I got the same error in my asp.net page. SO the problem appears to be authentication, but in the code I've added the address and the password, so I'm not too sure what it is on about.

0

I beleive Gmail works on port 587 not 25. Have you tried to change that port and also include credentials?

smtp.Credentials = new System.Net.NetworkCredential("yourEmail@gmail.com", "yourPassword");
smtp.Port = 587;
0

yep, the credentials are already there, and I changed the port too, from 25 to 587, no joy...and same error

0

OK, looks like we got it to work...why I don't know, and it's rather interesting.
I must have mentioned in one of the posts that at the email specified I received a few emails saying that access to my account had been blocked bla bla, and there is one option in gmail, not sure how many are aware of it, that essentially allows you to enable/disable "less secure app access" to it. Mine was disabled, and so I enabled, but still my darn code didn't work. Here is it again by the way, just for reference:

protected void submitData(object sender, EventArgs e) {
        //send email
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 25);
        smtp.Credentials = new System.Net.NetworkCredential("bassabasso@gmail.com", "xxxxxxx");
        smtp.UseDefaultCredentials = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;
        MailMessage mail = new MailMessage();
        mail.To.Add(new MailAddress("bassabasso@gmail.com"));
        mail.From = new MailAddress("bassabasso@gmail.com");
        mail.Subject = "test";
        mail.Body = "Hello this is a test";

        //smtp.Host = "smtp.gmail.com";


        smtp.Send(mail);
        //end of send email  
        }

Somebody suggested a few amendments to my code, merely put all the strings into variables, like so:

protected void submitData(object sender, EventArgs e) {
            //send email
            string Subject = "This is test mail using smtp settings";
            string Body = "Hello this is a test";
            string ToEmail = "bassabasso@gmail.com";
            string SMTPUser = "bassabasso@gmail.com", SMTPPassword = "xxxxx";
            SmtpClient smtp = new SmtpClient();

            //smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            MailMessage mail = new MailMessage();
            mail.To.Add(ToEmail);
            mail.From = new MailAddress(SMTPUser);
            mail.Subject = Subject;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            //if you are using your smtp server, then change your host like "smtp.yourdomain.com"
            smtp.Host = "smtp.gmail.com";
            //change your port for your host
            smtp.Port = 25; //or you can also use port# 587
            smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
            //smtp.Host = "smtp.gmail.com";


            smtp.Send(mail);
        }

And lo and behold, it damn works now...but I still have no flipping idea why

Edited by Violet_82

0

Well... it looks line 5 in the first sample is now commented on line 9 in the second sample. I missed that in your previous post.

if you specific smtp.UseDefaultCredentials equal to true, then your smtp.Credentials property isnt going to be used.

0

Yep, but it made no difference as I tried with and without that...that's why I;m puzzled.
Could it be the order, that's the only significant difference...

Edited by Violet_82

0

No the order doesn't matter. You are just assigning values to properties. The event happens when you execute the method to send.

If I have some time I'll try to run your code on a sample site.

0

Ah OK, then I have no idea. Oh don't worry mate, I don't want to waste your time, now I know that the other version works so it's OK :-)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.