locked
Send Email activity with Sharepoint workflow visual studio RRS feed

  • Question

  • I've been trying to get the SendEmail activity in Visual Studio 2010 to work with my workflow, but to no avail. I've configured my outgoing email settings in Central Admin > System Settings > Configure outgoing email settings, using my school's SMTP server. I've also configured the SMTP Email settings in IIS Manager to use the same SMTP server. As for the workflow activity, I tried 2 methods.

    Method 1 uses the code below which gives me an "Error Occurred" under my workflow status, instead of "completed".

    SPUtility.SendEmail(SPContext.Current.Web, false, false, "to_address@mail.com", "Job Application Rejected", "Rejected");

    Method 2 involves entering the values into the activity properties window, which allows my workflow to be completed, but the workflow history puts the error "The e-mail message cannot be sent. Make sure the outgoing e-mail settings for the server are configured correctly."

    I'm not sure if I missed out any steps to configure my Workflow settings. Any help would be greatly appreciated. Thanks

      

    Sunday, November 6, 2011 9:49 AM

Answers

All replies

  • I believe, you have to check the SMTP settings. Before come to SharePoint, we will first trace down the issue. [And go to web site in central administration from Manage web application and from general settings check the outgoing settings.]

    What you have to do is, check whether the SMTP server sending emails or not.

    For checking the email whether it is sending or not, use 

    http://praveenbattula.blogspot.com/2009/04/how-to-check-smtp-is-working-or-not.html

    If emails are sending out from SMTP then you have to check the event viewer and SharePoint error logs for finding the exact issue.


    ASP.NET and SharePoint developer
    Blog: http://praveenbattula.blogspot.com
    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    • Proposed as answer by R0ber7oMVP Sunday, November 6, 2011 12:09 PM
    Sunday, November 6, 2011 11:55 AM
  • Avoid using SPContext.Current.Web in your custom workflow activities.

    In my practice I've noticed that SharePoint runs your workflows under completely different processes. Your workflow activities can be executed under:
     • IIS worker process,
     • owstimer.exe process
     
    In respect to the event that triggered the workflow SharePoint selects the process that will actually execute it. Workflow when triggered from ASP.NET (i.e. the IIS worker process) is automatically rescheduled to run under owstimer.exe.

    The result is you cannot use SPContext.Current.

    In all your custom workflow activities you have to use a WorkflowContext instance which provides a Web property.

    Your should declare a dependency property of type WorkflowContext to get access to it.  VS item template will provide you with the necessary code.

    EG:

        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(MyCstomEmalActivity));
    
        [Browsable(true)]
        [ValidationOption(ValidationOption.Required)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }
    


    In most cases only source code can help solving problems.
    • Proposed as answer by HeToC Sunday, November 6, 2011 2:40 PM
    Sunday, November 6, 2011 2:39 PM
  • Hi HeToc,

    I'm getting some error with the code. Here is my code which I used.

    public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(SendEmail)); //I'm not sure if using "SendEmail" is correct

    SPUtility.SendEmail(__Context.Web, false, false, "cgchen.2009@sis.smu.edu.sg", "Job Application Rejected", "Rejected"

     

    ); //this line is in my SendEmail method invoking code

    Here is the error I got.

    Could not create activity of type 'JobApplicationApproval.Workflow1.Workflow1'. System.ArgumentException: Type 'Microsoft.SharePoint.WorkflowActions.SendEmail' does not define a static dependency property with name '__ContextProperty'.

    Parameter name: ownerType

       at System.Workflow.ComponentModel.DependencyProperty.ValidateAndRegister(String name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, Type validatorType, Boolean isRegistered)

       at System.Workflow.ComponentModel.DependencyProperty.Register(String name, Type propertyType, Type ownerType)

       at JobApplicationApproval.Workflow1.Workflow1..cctor()

    On a side note, is it possible to use system.net.mail instead?

    Thanks for your help :)



    • Edited by mark_chen25 Monday, November 7, 2011 6:59 AM
    Monday, November 7, 2011 6:06 AM
  • Could not create activity of type 'JobApplicationApproval.Workflow1.Workflow1'. System.ArgumentException: Type 'Microsoft.SharePoint.WorkflowActions.SendEmail' does not define a static dependency property with name '__ContextProperty'.

    SendEmail activity is allready defined in core SP library. so consider to rename your activity or add your namespace when needed and dont forget about xml files 

    PS: when you're defining a dependency property third parameter should target to your custom activity type.


    In most cases only source code can help solving problems.
    Monday, November 7, 2011 8:04 AM
  • I believe, you have to check the SMTP settings. Before come to SharePoint, we will first trace down the issue. [And go to web site in central administration from Manage web application and from general settings check the outgoing settings.]

    What you have to do is, check whether the SMTP server sending emails or not.

    For checking the email whether it is sending or not, use 

    http://praveenbattula.blogspot.com/2009/04/how-to-check-smtp-is-working-or-not.html

    If emails are sending out from SMTP then you have to check the event viewer and SharePoint error logs for finding the exact issue.


    ASP.NET and SharePoint developer
    Blog: http://praveenbattula.blogspot.com
    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.


    There are some problems when following the steps with different SMTP servers.

    My school's SMTP server:

    I get this error “530 5.7.1 Client was not authenticated”

    When I googled for this error, the solutions asked me to go into Exchange Management Shell, which I do not have.

     

    Using Gmail or Hotmail’s SMTP servers:

    I get kicked out of the session right after typing in the starttls command

    Is there another alternative way to test it?

    Monday, November 7, 2011 8:04 AM
  • Could not create activity of type 'JobApplicationApproval.Workflow1.Workflow1'. System.ArgumentException: Type 'Microsoft.SharePoint.WorkflowActions.SendEmail' does not define a static dependency property with name '__ContextProperty'.

    SendEmail activity is allready defined in core SP library. so consider to rename your activity or add your namespace when needed and dont forget about xml files 

    PS: when you're defining a dependency property third parameter should target to your custom activity type.


    In most cases only source code can help solving problems.


    I'm new to Sharepoint and Visual Studio, so thanks for patiently guiding me. When I use the name of my sendEmail activity (eg. sendEmail2), I get this error

    'JobApplicationApproval.Workflow1.Workflow1.sendEmail2' is a 'field' but is used like a 'type' 

    As for the XML files, what do I need to change?

    Monday, November 7, 2011 8:11 AM
  • Here is a great article about creating custom WF activitiy with demo source code included:  http://blog.symprogress.com/2011/01/spd-2010-custom-workflow-activity/

    Please have a look and try to implement your activity by the same way - you've got lots of experience.

     


    In most cases only source code can help solving problems.
    • Marked as answer by Shimin Huang Friday, November 18, 2011 8:20 AM
    Monday, November 7, 2011 8:27 AM