Sending e-mails

Discussion in 'C#' started by Flyers8985, Mar 3, 2008.

  1. #1
    I'm pretty new to .NET, but I after a user registers for my site I would like to send out a confirmation e-mail. I searched around for a good while looking for code and I came up with this.

    dim smtpServer as String
    smtpServer = "mail.mydomain.com"
    
    dim cdoSendUsingPort as Integer
    cdoSendUsingPort = 2
    
    dim cdoBasic as Integer
    cdoBasic = 1
    
    dim userName as String
    userName = "userName"
    
    dim password as String
    password = "password"
    
    dim msg As MailMessage = new MailMessage()
    msg.Subject = "Testing"
    msg.Body = "Hello World"
    msg.From = "something@something.com"
    msg.To = "something@something.com"
    
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer)
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25)
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort)
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic)
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName)
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password)
    
    System.Web.Mail.SmtpMail.SmtpServer = smtpServer
    System.Web.Mail.SmtpMail.Send(msg)
    Code (markup):
    And I am receiving an error message that looks like this which is pointing to the last line - The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available

    I'm not too sure what that means, but any help or insight would be greatly appreciated.

    Thanks again, Flyers8985!
     
    Flyers8985, Mar 3, 2008 IP
  2. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    might be an idea to change some of the dummy values to real ones - especially line 2 to the name of your domain and msg.from to an address at your domain
     
    Free Born John, Mar 3, 2008 IP
  3. Flyers8985

    Flyers8985 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Oh, I did. I just well, didn't want to throw some of the values in there, as I was testing it with personal e-mail addresses and such.
     
    Flyers8985, Mar 3, 2008 IP
  4. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    then perhaps the smtp server is disabled in IIS or needs credentials setting.
     
    Free Born John, Mar 4, 2008 IP
  5. Jhar

    Jhar Peon

    Messages:
    318
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Seems like the code you are using is from an older version of .net. If your host has .net 2.0 your can try:

    Sub Authenticate()
    'create the mail message
    Dim mail As New MailMessage()

    'set the addresses
    mail.From = New MailAddress("me@mycompany.com")
    mail.To.Add("you@yourcompany.com")

    'set the content
    mail.Subject = "This is an email"
    mail.Body = "this is the body content of the email."

    'send the message
    Dim smtp As New SmtpClient("127.0.0.1")

    'to authenticate we set the username and password properites on the SmtpClient
    smtp.Credentials = New NetworkCredential("username", "secret")
    smtp.Send(mail)
    End Sub 'Authenticate
     
    Jhar, Mar 7, 2008 IP
  6. lofvenhamn

    lofvenhamn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Please note that if you use the new version of e-mail processing in .NET 2.0, you must use System.Net.Mail instead of the old System.Web.Mail.
     
    lofvenhamn, Mar 17, 2008 IP
  7. docluv

    docluv Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Make sure your SMTP server does allow you to send e-mail from the mail server. You may need to login or pop before you send anything. Many SMTP servers do this to cut down on SPAM, etc.
     
    docluv, Mar 17, 2008 IP