Unable to send e-mail from "Contact Us" form via DASP SMTP

Discussion in 'ASP.NET / ASP.NET Core' started by rearndt, Jan 12, 2009.

  1. I have a "Contact Me" page on my DASP-hosted website to receive comments from viewers. However, the mail does not get relayed through the DASP SMTP server to the destination address. I have it configured per instructions in the KB articles.

    My web.config settings are :

    <system.net>
    <mailSettings>
    <smtp from="">
    <network host="smtp.mydomain.net" userName="username" password="password" port="25" />
    </smtp>
    </mailSettings>
    </system.net>

    The C# code in my submit button click event handler is :

    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress(email.Text.Trim());
    mailMessage.To.Add(new MailAddress("[email protected]"));
    mailMessage.Subject = subject.Text.Trim();
    mailMessage.Body = message.Text.Trim();
    mailMessage.IsBodyHtml = false;
    SmtpClient smtpClient = new SmtpClient("localhost");
    smtpClient.Send(mailMessage);

    Can anyone spot what I am doing wrong?

    Thank you very much!
     
  2. Hi Bob,


    This is what I've got and it works for me;


    Web.config;


    <system.net>
    <mailSettings>
    <smtp>
    <network host="localhost" port="25"/>
    </smtp>
    </mailSettings>
    </system.net>


    I set up an email_handler class as follows (vb.net);


    Imports System.Net.Mail
    Imports System.Net


    Public Class email_handler
    Public Sub New()


    End Sub


    Public Function sendEmail(ByVal which As String, ByVal email As String, ByVal subject As String, ByVal htmlbody As String)
    Dim from_address = "[email protected]"
    Dim to_address = email
    Dim mm As New MailMessage(New MailAddress(from_address),New MailAddress(to_address))
    mm.Body = htmlbody
    mm.Subject = subject
    mm.IsBodyHtml = true


    Dim smtp As New SmtpClient
    smtp.Host = "localhost"
    smtp.Send(mm)


    mm = Nothing
    End Function
    End Class


    Hope this helps.


    Cheers,


    Stuart
     
  3. Stuart,


    Thank you for your response. That is a very good way to do it. However, I found that my problem was that Ihad been using the "PostBackURL" property of the button control to move to another page displaying a thank you message to the user. Apparently, that command executes first and prevents the button click event handler from sending the mail message. I pulled that attribute out of the button declaration and was able to submit an email message from the form.


    But I like your idea of an email handler class. I think I'll implement the same thing.


    I do appreciate your help!


    Bob Arndt
     

Share This Page