[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

C# using from with .net mail class

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\n");
                    strMsg.Append(p_strParams[5]);
                    strMsg.Append("\n\nBuyer's email: " + strBuyerEmail);
                    strMsg.Append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
                    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();
                    client.Send(msg);

Open in new window

0
jonatec
Asked:
jonatec
  • 2
  • 2
  • 2
  • +4
1 Solution
 
guru_samiCommented:
can you try inserting try catch and debug messages:

try {
        client.Send(msg);
      }
      catch (Exception ex) {
        Response.Write("Exception caught in CreateCopyMessage(): {0}",
                    ex.ToString() );
        }
Or any other messages.
0
 
käµfm³d 👽Commented:
You can install and run the Remote Debugger on the Web Server (if permitted by admin).

   Installation File:
   http://www.microsoft.com/downloads/details.aspx?FamilyID=440ec902-3260-4cdc-b11a-6a9070a2aaab&displaylang=en

   Instructions:
   http://msdn.microsoft.com/en-us/library/bt727f1t.aspx
0
 
Éric MoreauSenior .Net ConsultantCommented:
is the body of your e-mail correctly shows the strBuyerEmail?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Client.Port = 8025;

that might work for you...
0
 
zadeveloperCommented:
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.
0
 
zadeveloperCommented:
if you dont know how to send mail via telnet try this:
start->run->telnet
open mail.domain.com 25
220 welcome to mx1.zadeveloper.com
helo me.zadeveloper.com
250 Hello.
MAIL FROM: admin@zadeveloper.com
550 The address is not valid.
0
 
Tony McCreathTechnical SEO ConsultantCommented:
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.

0
 
jonatecAuthor Commented:
* 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?
0
 
Tony McCreathTechnical SEO ConsultantCommented:
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.

0
 
jonatecAuthor Commented:
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.

Cheers.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now