how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

Last post 03-17-2008 4:38 PM by cluce. 4 replies.

Sort Posts:

  • how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

    03-17-2008, 4:08 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    I used this function succesfully internally without using a username and password but now that the website is being hosted outside the network i need to specify a username and password to connect to the mail server. can someone tell me how to incorporate this in my code. I know you need to set it in the web.config file but I am unable to tie that into the function below.   I am not familar with the events or methods that use the setting in teh web.config can someone help me with this? thanks.

    Function:

    Private Sub SendMail(ByVal from As String, ByVal body As String)

    'procedure that passes a string through the configured mail server
    Dim mailServerName As String = "mail.domain.com"
    Dim message As MailMessage = New MailMessage(txtEmail.Text, "cluce@domain.com", "Technical Support Form", body)
    Dim mailClient As SmtpClient = New SmtpClient
    mailClient.Host = mailServerName
    mailClient.Send(message)
    message.Dispose()
    End Sub

    web.config

    <system.net>
    <
    mailSettings>
    <
    smtp from=cluce@domain.com>
    <
    network host="mail.domain.org" password="password" userName="username" />
    </
    smtp>
    </
    mailSettings>
    </
    system.net>

  • Re: how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

    03-17-2008, 4:21 PM
    Answer
    • Participant
      808 point Participant
    • dprior
    • Member since 11-23-2007, 5:00 PM
    • Posts 151

     If you have the data correct in your web.config file, all you need to do is comment out mail.client.Host = mailServerName.  The settings specified in the config file will automatically be loaded when you create a new SmtpClient.

    http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx 

    Please remember to mark any helpful responses as answers.
  • Re: how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

    03-17-2008, 4:30 PM
    Answer
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    yes, I believe I have my web.config configured properly.  So all I do is this and my smtp settings should be recognized. 

    Function:

    Private Sub SendMail(ByVal from As String, ByVal body As String)

    'procedure that passes a string through the configured mail server

    Dim message As MailMessage = New MailMessage(txtEmail.Text, "cluce@domain.com", "Technical Support Form", body)
    Dim mailClient As SmtpClient = New SmtpClient

    mailClient.Send(message)
    message.Dispose()
    End Sub

    'sends string through email

    Dim body As String

    body = _

    "Name: " & txtName.Text.Trim & _

    ControlChars.CrLf & _

    "Email: " & txtEmail.Text.Trim & _

    ControlChars.CrLf & _

    ControlChars.CrLf & _

    "Comments/requests: " & _

    ControlChars.CrLf & _

    ControlChars.CrLf & _

    txtComments.Text.Trim

    SendMail(txtEmail.Text.Trim, body)

  • Re: how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

    03-17-2008, 4:35 PM
    • Participant
      808 point Participant
    • dprior
    • Member since 11-23-2007, 5:00 PM
    • Posts 151

     Yes, that'll do.

    Please remember to mark any helpful responses as answers.
  • Re: how can I use this smtp function to submit my forms through email with a username , password and host in my web.config file

    03-17-2008, 4:38 PM
    • Member
      89 point Member
    • cluce
    • Member since 01-25-2008, 5:37 PM
    • Posts 410

    yeah, thanks. I just tested it. unfortunatley the external mail server still dont work but thats has nothing to do with my code because this works with our internal mail server just fine.

Page 1 of 1 (5 items)