C# using from with .net mail class

Posted on 2009-12-30
Last Modified: 2013-11-30
I am using System.Net.Mail ok in sending emails with SMTP. However the problem I have is I can't get the .From method to work. Whenever I set the .From method the email doesn't arrive, although I'm not sure if it actually gets sent. I can't step through the code because I have to run it on the web server to process the email. Any ideas please? I'm developing using VS 2008.

StringBuilder strMsg = new StringBuilder();

                    strMsg.Append("(  This is an automated email, please don't reply to it.  )\n");



                    strMsg.Append("\n\nBuyer's email: " + strBuyerEmail);


                    strTmp += fnHtmlEncode(strMsg.ToString());

                    strTmp += "Ad Content:\n" + strAdContent;

                    MailMessage msg = new MailMessage();

                    msg.From = new MailAddress(strBuyerEmail);


                    msg.To.Add(new MailAddress(strSellerEmail));

                    msg.Subject = strSubject;

                    msg.IsBodyHtml = false;

                    msg.Body = strTmp;

                    SmtpClient client = new SmtpClient();


Open in new window

Question by:jonatec
    LVL 41

    Expert Comment

    can you try inserting try catch and debug messages:

    try {
          catch (Exception ex) {
            Response.Write("Exception caught in CreateCopyMessage(): {0}",
                        ex.ToString() );
    Or any other messages.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    You can install and run the Remote Debugger on the Web Server (if permitted by admin).

       Installation File:

    LVL 69

    Expert Comment

    by:Éric Moreau
    is the body of your e-mail correctly shows the strBuyerEmail?
    LVL 3

    Expert Comment

    make sure you're using your correct smpt port...

    Client.Port = 8025;

    that might work for you...
    LVL 13

    Expert Comment

    Also - the smtp server you are using could have relay access restrictions on the from user. Try starting a telnet session and manyually sending the smtp commands to the server , once you put in the send address the server may give you the error code.
    If its working without the sender I would guess that all your ports and access is setup correctly.
    LVL 13

    Expert Comment

    if you dont know how to send mail via telnet try this:
    open 25
    220 welcome to
    250 Hello.
    550 The address is not valid.
    LVL 23

    Expert Comment

    I've had trouble trying set the From address entered by users input. Emails can quite often be rejected. e.g. if the email address is invalid, or does not resolve to a known domain.


    Author Comment

    * Tried a Try.. Catch and the .Send method does not raise an exception.

    * Can't run the remote debugger because it's running on a webhost.

    * The body of strBuyerEmail gets set correctly.

    * In my Web.Config file the port number given to me is: 25, should I change to 8025 ? Why would that affect the use of the .Send method in C# ?

    * Use of Telnet. Presumably I would have to get onto the web server to try this, however I am using a Web Hosting  company and therefore have restricted access.

    * Email address is valid and in the correct format.

    Should I conclude it is a restriction in SMTP and contact my Web Hosting company?
    LVL 23

    Accepted Solution

    My problem was related to mail servers rejecting emails based on the From address so the email gets sent fine, just never reaches its destination.

    Your web hosting company may have a restriction that the from address has to be from the host you own. So maybe it is them.


    Author Closing Comment

    Yes you are absolutely right. I contacted my web hosting company and was told the From address must match an email address for the domain being sent from, otherwise it's rejected as a spoof address.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now