MailMessage not working on server

Last post 11-06-2009 5:55 PM by hapax_legomenon. 8 replies.

Sort Posts:

  • MailMessage not working on server

    11-05-2009, 9:26 AM

    I wrote some code to send email using the MailMessage object. It's working fine in localhost, but when I run the code on my development server I get no email.

    What do I need to do to set up mail on the server?

  • Re: MailMessage not working on server

    11-05-2009, 11:38 AM
    • All-Star
      25,515 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,366

    Hi,

    Any error.??

    Or

    Is Smtp is open for sendin mail

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: MailMessage not working on server

    11-05-2009, 1:11 PM
    • Contributor
      2,536 point Contributor
    • tamilcodes
    • Member since 04-30-2009, 5:28 PM
    • Posts 525

    ask your hosting provider about the smtp port, sometimes you may need to use 2525 as the port 

  • Re: MailMessage not working on server

    11-05-2009, 1:16 PM
    Answer
    • Member
      264 point Member
    • arjit.malviya
    • Member since 11-01-2009, 6:53 PM
    • India
    • Posts 42

    I m writing a sample code for sending mail. Please recheck your mail coding...


    using System.Net.Mail

            MailMessage msgMail = new MailMessage();
            MailMessage myMessage = new MailMessage();
            myMessage.From = new MailAddress("sender's email");
            myMessage.To.Add("recipient's email");
            myMessage.Subject = "Subject";
            myMessage.IsBodyHtml = true;
            myMessage.Body = "Message Body";

            SmtpClient mySmtpClient = new SmtpClient();
            System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("
    email", "password");
            mySmtpClient.Host = "your smtp host address";
            mySmtpClient.
    UseDefaultCredentials = false;
            mySmtpClient.Credentials = myCredential;
            mySmtpClient.ServicePoint.
    MaxIdleTime = 1;
            mySmtpClient.Send(myMessage);
            myMessage.Dispose();
     
     
    For more information about sending mail using ASP.NET 3.5, see this article http://www.godotnet.org/2009/11/sendusing-configuration-value-is.html

     

    Microsoft Student Partner.
    Microsoft Certified Technology Specialist.
    .NET Recipes http://www.godotnet.org

    Please mark this post as ANSWER if it helped you!
    Filed under:
  • Re: MailMessage not working on server

    11-05-2009, 7:49 PM
    Answer
    • Member
      58 point Member
    • Manivelan
    • Member since 07-13-2009, 12:30 PM
    • Posts 9

    Hi,

    I have given a sample code below which would work from the development server. (if your company is using Microsoft exchange server)

    Step 1:

    Public Sub SendEmail(ByVal Toaddress as string, ByVal body as string, ByVal subject as string, ByVal ishtml as Boolean)

     

                Dim Msg As New MailMessage


                            Try  

                      Msg.From = New MailAddress(ConfigurationManager.AppSettings("EmailFromAddres").ToString, 

                                         ConfigurationManager.AppSettings("EmailFromAddressDisplayName").ToString)

                      Msg.To.Add(New MailAddress(Toaddress))

                       Msg.Subject = subject

                      Msg.Body = body

                      Msg.IsBodyHtml = ishtml

                      Msg.Priority = MailPriority.Normal

                      ' to connect to the exchange server by using info in the web.config

                      Dim client As New SmtpClient(ConfigurationManager.AppSettings("ExchangeServerName").ToString())

                client.DeliveryMethod = SmtpDeliveryMethod.Network

                      client.UseDefaultCredentials = True

                      client.Port = ConfigurationManager.AppSettings("ExchangeServerPort").ToString()

                      client.Timeout = ConfigurationManager.AppSettings("ExchangeServerTimeout").ToString()

                      'send the email

                      Try

                            client.Send(Msg)

                      Catch ex As Exception

     

                            Exit Sub

                      End Try

     

                Catch ex As Exception

     

                Finally

                      Msg = Nothing

                End Try

     

          End Sub


    Step 2:


    change your web.config to include the following information about your company's exchange server


    <configuration>
         <appSettings>
            <add key="ExchangeServerName" value="CWcwcw"/>
            <add key="ExchangeServerPort" value="25"/>
            <add key="ExchangeServerTimeout" value="999999"/>
            <add key="EmailFromAddres" value="something@somedomain.com"/>
            <add key="EmailFromAddressDisplayName" value="Mr Someone"/>
            
        </appSettings>


    </configuration>


    Hope this helps.

    Please let me know if you need any other help.



    Manivelan
    Web Developer
    U.K

    (Please Mark it as answer if my sugesstion helps you)
  • Re: MailMessage not working on server

    11-05-2009, 7:52 PM
    • Member
      58 point Member
    • Manivelan
    • Member since 07-13-2009, 12:30 PM
    • Posts 9

    Sorry forgot to mention the below information:

    please include the below lines at the top of the code behind page.

    Imports System.Net.Mail
    Imports System.Web.Configuration
    Imports System.Configuration
    Imports System.Net.Configuration

    Manivelan
    Web Developer
    U.K

    (Please Mark it as answer if my sugesstion helps you)
  • Re: MailMessage not working on server

    11-05-2009, 8:01 PM
    • Member
      220 point Member
    • Ryan_A
    • Member since 10-28-2009, 12:06 AM
    • Rochester, NY
    • Posts 45

    What I've found in the past is that when I push to my development server, if the sender and receiver of the email are the same address, it will generally fail.  Try updating one or the other to see if that takes care of it.  You could also just try some different recipient addresses to see if they receive the emails.


    Ryan Andreotta, MCPD
    http://www.rackwire.com
    Shared hosting from $4.99/month
    VPS from $22.00/month
  • Re: MailMessage not working on server

    11-06-2009, 5:23 AM
    Answer
    • Member
      414 point Member
    • urstop
    • Member since 12-04-2007, 3:24 AM
    • Posts 93

    On the shared hosting, most of the times they use a different smtp server other than the server they host your websites. So you cannot use local host as the SMTP server. You will have to find out and get the correct smtp server name from your host. Also, sometimes these smtp servers need a username/password and will not work with the default credentials. So ask your hosting provider for this information.

    Finally, some hosters allow only mails to be sent from a Email address that is in the email accounts of the domain that you have hosted with them. So you might not be able to use different email address.

  • Re: MailMessage not working on server

    11-06-2009, 5:55 PM

    Thanks for all the replies. My problem was that the SMTP server setting in my web.config was not correct for the remote server.

Page 1 of 1 (9 items)