locked
Sending e-mail with event handler RRS feed

  • Question

  • Hello thanks for reading my question. I am trying to use the itemAdded event handler to send e-mail to myself. I wrote a function called SendEMail with csharp. This function works perfectly if I put it in a windows form application, but it would not work if I put it in a sharepoint event handler, the error msg it gave me was "failure to send e-mail". Can anyone tell me how to correctly e-mail in sharepoint programmatically? Thanks

    My code is like this:
     
    1        public String SendEMail 
    2   (string from,   // e.g., "sender@csharp-online.net" 
    3    string to,     // e.g., "receiver@csharp-online.net" 
    4    string subject,// e.g., "Please read this!" 
    5    string body)   // e.g., "Thanks for the memories." 
    6        { 
    7            try 
    8            {  // Construct a new e-mail message  
    9                SmtpClient client = new SmtpClient("smtprelay"); 
    10                client.EnableSsl = false
    11                client.Send(from, to, subject, body); 
    12                return "Success"
    13            } 
    14            catch (SmtpException ex) 
    15            { 
    16                return ex.Message.ToString(); 
    17            } 
    18            catch (InvalidOperationException ioe) 
    19            { 
    20                return ioe.Message.ToString(); 
    21            } 
    22        } 

    Tuesday, January 20, 2009 7:24 PM

Answers

All replies

  • You need to use SPUtility.SendEmail method. This will be simple to use method. It will utilize SMTP IP configured for sharepoint web application.  We can always set SMTP Outgoing settings in web application settings in Central admin. Here is the SendEmail method sample code on MSDN.

     http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail.aspx
    • Proposed as answer by Moonis Tahir Wednesday, January 21, 2009 7:39 PM
    • Marked as answer by SPNoob Wednesday, January 21, 2009 8:28 PM
    Tuesday, January 20, 2009 8:29 PM
  • Thanks, I used the code from msdn exactly and my VS2008 gave me 2 compile error:

    Error    1    The type 'System.Web.UI.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.    C:\VSProjects\EventHandlerSolutionStarterKit\MOSS.EventHandlers\ItemHandler.cs    302    17    MOSS.EventHandlers

    Error    2    The name 'Context' does not exist in the current context    C:\VSProjects\EventHandlerSolutionStarterKit\MOSS.EventHandlers\ItemHandler.cs    302    57    MOSS.EventHandlers


    1        public String SendEMail 
    2   (string from,   // e.g., "sender@csharp-online.net" 
    3    string to,     // e.g., "receiver@csharp-online.net" 
    4    string subject,// e.g., "Please read this!" 
    5    string body)   // e.g., "Thanks for the memories." 
    6        { 
    7            try 
    8            { 
    9                SPWeb thisWeb = SPControl.GetContextWeb(Context); 
    10                bool success = SPUtility.SendEmail(thisWeb, truetrue, to, subject, body); 
    11                if (success) 
    12                { 
    13                    return "Success"
    14                } 
    15                else 
    16                { 
    17                    return "Failed to send email"
    18                } 
    19            } 
    20            catch (Exception ex) 
    21            { 
    22                return ex.Message.ToString(); 
    23            } 
    24        } 

    Tuesday, January 20, 2009 8:53 PM
  • since you are inside event handler, you should get the SPWeb through Properties object which is
    _properties.web


    SPWeb thisWeb = _properties.web;
    Tuesday, January 20, 2009 8:56 PM
  • Moonis Tahir said:

    since you are inside event handler, you should get the SPWeb through Properties object which is
    _properties.web


    SPWeb thisWeb = _properties.web;


    It now compiles successfully, but I tried to trigger it on my sharepoint site, it still wouldn't send e-mail and actually stops code execution at the line that suppose to send e-mail, how do I debug this?

    1using (SPWeb thisWeb = properties.OpenWeb()) 
    2
    3    success = SPUtility.SendEmail(thisWeb, truetrue, to, subject, body); 
    4

    Tuesday, January 20, 2009 9:37 PM
  • to debug it, please see this thread, you will actually attach the w3wp process to debugger but there are some steps you need to do, please go through the following thread.

    http://www.sharepointu.com/forums/p/1858/5085.aspx

    i hope you did make sure that SMTP outgoing is properly configured in central admin in your web application settings. if it is there, you can test it by configuring alert on a document library for new documents. just add a document to library and you should get an email. if that works, your code should also work. If alerts are not working, you may have to check the LOGs to find out the reasons.
    Tuesday, January 20, 2009 10:47 PM
  • Moonis Tahir said:

    to debug it, please see this thread, you will actually attach the w3wp process to debugger but there are some steps you need to do, please go through the following thread.

    http://www.sharepointu.com/forums/p/1858/5085.aspx

    i hope you did make sure that SMTP outgoing is properly configured in central admin in your web application settings. if it is there, you can test it by configuring alert on a document library for new documents. just add a document to library and you should get an email. if that works, your code should also work. If alerts are not working, you may have to check the LOGs to find out the reasons.


    I tried to set up the alert, it looks like it doesn't allow me to put just any e-mail address there, it has to be an existing user? My user account doesn't have an e-mail associated with it. How do I send e-mail to any e-mail address?
    Wednesday, January 21, 2009 2:34 PM
  • to test if sharepoint is sending alert or not, you can put an email to your user profile and get the alert on your email. Once you see the alert coming fine, you can then test SendEmail to send email to any other email add.

    One question i have is you said your email using SMTPClient does work in windows form app on the same sharerpoint server while it does not work in Sharepoint event handler. Try to set client.Host ="yourSMTPIP_OR_ServerName"; property explicitly. i am asking you to try this if you think other route of alerts testing will take you longer. so you may try this if it works, if you get error, you need to check the event log and strack trace to reach the root cause.
    Wednesday, January 21, 2009 2:50 PM
  • Moonis Tahir said:

    to test if sharepoint is sending alert or not, you can put an email to your user profile and get the alert on your email. Once you see the alert coming fine, you can then test SendEmail to send email to any other email add.

    One question i have is you said your email using SMTPClient does work in windows form app on the same sharerpoint server while it does not work in Sharepoint event handler. Try to set client.Host ="yourSMTPIP_OR_ServerName"; property explicitly. i am asking you to try this if you think other route of alerts testing will take you longer. so you may try this if it works, if you get error, you need to check the event log and strack trace to reach the root cause.


    Sorry I was wrong, the windows form application works on my local machine, but did not work on the sharepoint server, it get the same error "Failure sending e-mail". Where is the logs?
    Wednesday, January 21, 2009 3:14 PM
  • it is actually your SMTP relay issue then. On your local machine, whatever SMTP HOST address you are using is working but it is not accessible from any other machine. You can setup Virtual SMTP in IIS. see this link

    http://www.cmsconnect.com/praetor/webhelpg2/Chapter_2_-_Pre-installation_considerations/Configuring_the_SMTP_Server.htm
    • Proposed as answer by Moonis Tahir Wednesday, January 21, 2009 7:39 PM
    • Marked as answer by SPNoob Wednesday, January 21, 2009 8:28 PM
    Wednesday, January 21, 2009 3:46 PM