Sending emails in asp.net

Discussion in 'Site Programming, Development and Design' started by lionelthomas, Oct 12, 2009.

  1. Am using this example:

    protected void Page_Load(object sender, EventArgs e)
    {
    //create the mail message
    MailMessage mail = new MailMessage();

    //set the addresses
    mail.From = new MailAddress("postmaster@mydomain.com");
    mail.To.Add("postmaster@mydomain.com");

    //set the content
    mail.Subject = "This is an email";
    mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
    //send the message
    SmtpClient smtp = new SmtpClient("mail.mydomain.com");

    NetworkCredential Credentials = new NetworkCredential("postmaster@mydomain.com", "password");

    smtp.Send(mail);
    lblMessage.Text = "Mail Sent";
    }

    This script worked when the "mail.To.Add" is set to an authenticated email.
    Cannot send an email to an unauthenticated email such as "lionelthomas46@gmail.com"

    How does one send such an email?
    Regards.
     
  2. Ray

    Ray

    What is the error message you're getting? Did you try using another email other then a gmail account?
     
  3. Im not sure if I am missing it, plus I know it is an example. But have you checked that you set smtp.Credentials = Credentials?
     
  4. There is no error message.
    Am able to send an email from any email address to an email address that is in my domain ie. address@lionelthomas.com. So, I have set up a contact page at www.lionel thomas.com that works(not yet pretty) .But when I send an email to any address that is not in my domain then it executes but the email is not received. Have tried several.
    regards.
     
  5. Ray

    Ray

    Try sending to another email address that is not gmail. Make sure the from address is valid. Remember the From address or also the 'Reply-To' address is also important because it is the email address the server will use if it needs to send a bounce back message. Bounce back messages are very helpful when troubleshooting email problems.
     
  6. Ray

    Ray

    The error message does indicate that you are not passing the correct SMTP authentication. Remember, before you can send out through our SMTP server, the SMTP server will have to authenticate you. Try looking at this kb article again as an example code to send out email through your web pages.

    http://support.Winhost.com/KB/a650/how-to-send-email-in-aspnet.aspx
     
    Last edited by a moderator: Oct 14, 2015
  7. It appears that the from address can be any string in email address format.

    Sent an email from aaaabbbbbb@cccccc.com to username@lionelthomas.com (valid address in my domain)

    Email received with aaaabbbbbb@cccccc.com in From: field


    My point exactly. The "to address" has to be authenticated but the "from address" doesn't.
    I want to send an email from postmaster@lionelthomas.com to joeuser@anydomain.com.
    Is there a mode that will authenticate the "from address" to allow me to send to an unauthenticated "to address"
     
  8. As Ray said you the SmtpClient is likely not properly logging into the SMTP server, which means that the SMTP server will not relay the message to another domain. Which is why username@lionelthomas.com works, but username@gmail.com does not.

    Looking at the code snippet you pasted, the Credentials variable is not being assigned to the smtp client.

    If you change:

    NetworkCredential Credentials = new NetworkCredential("postmaster@mydomain.com", "password");

    to:
    NetworkCredential Credentials = new NetworkCredential("postmaster@mydomain.com", "password");
    smtp.Credentials = Credentials;

    then if they have things set up the way I would expect it should allow you to send mail to username@gmail.com
     
  9. Thanks Sean Lynch,
    How does one learn this stuff? Is there a place I can read about this without having to tiptoe through the minefield. It takes me hours to learn by trial and error.
    So the SMTP client needs to be authenticated and not just the addresses.
    Regards,
    Lionel
     
  10. Not really sure, I have learned a large part with trial and error.

    http://stackoverflow.com is a good place for questions like this.

    And Ray, your KB article has the same problem I pointed out. Since the code that Lionel posted is from the KB article that you posted.

    Edit: Only the C# version of it, the VB.Net one is right.
     
  11. Ray

    Ray

    Thanks Sean, I'll make sure to update our KB article.
     

Share This Page