You are viewing limited content. For full access, please sign in.

Question

Question

Using System.Net.Mail.SmtpClient inside a script activity

asked on October 28, 2019

I have a script in one of my workflows that sends an email using System.Net.Mail.SmtpClient.

I cannot use Workflow's default Email activity, because it doesn't allow changing some advanced options such as email and attachment headers.

My question: is the Workflow Server's email settings (the ones you configure in the WF Configuration Manager) exposed through the API? Otherwise I have to store credentials elsewhere (environment variables etc.) and I'd rather not do that because then we'd need to update a second thing if the server info changes.

Code snippet:

System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
smtpclient.Host = "mymailserver"; // Mail server hostname or IP
smtpclient.Port = 25;
smtpclient.Credentials = new System.Net.NetworkCredential("username@mailserver", "password"); // just an example, we don't hardcode credentials in scripts

I'd love to be able to replace these four lines with the SmtpClient workflow uses for its own Email activity.

0 0

Replies

replied on October 28, 2019 Show version history

Depending on how you have your environment configured and what outgoing address you're trying to use, you could just use the default credentials setting.

I have a workflow that for similar reasons required me to use a script activity, and I don't have the credentials hard-coded, I just set it to use default credentials, which will be the workflow service user.

Any email address the default activities are able to send from should work with this as well because the script would use the same account.

For example,

// Create new smtp client with server value
SmtpClient client = new SmtpClient(server);

 // Set default client credentials
client.UseDefaultCredentials = true;

NOTE: When executing the code from the script editor rather than running the workflow, it runs as the current user, not the service account, so that is something to keep in mind when testing.

1 0
replied on October 28, 2019

Out of curiousity, what are you trying to change in the email headers?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.