摘要: 微软动态CRM专家罗勇 ,回复303或者20190213可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。

当然,首先要确认 【设置】> 【电子邮件配置】 > 【电子邮件配置设置】中的 【允许在尚有未解析的电子邮件收件人的情况下发送邮件】设置为是,还有就是连接CRM使用的用户邮箱已经配置好,外发邮件已经测试通了,可以发邮件。

不多说了,上代码:

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.ServiceModel.Description; namespace SendEmailToEmailAddrs
{
class Program
{
static void Main(string[] args)
{
try
{
string inputKey;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"]));
AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials();
orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"];
orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"];
string needConfirm = ConfigurationManager.AppSettings["needConfirm"];
using (var orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
{
WhoAmIRequest whoReq = new WhoAmIRequest();
var whoRsp = orgSvc.Execute(whoReq) as WhoAmIResponse;
var userEntity = orgSvc.Retrieve("systemuser", whoRsp.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname"));
Console.WriteLine(string.Format("欢迎【{0}】登陆到【{1}】", userEntity.GetAttributeValue<string>("fullname"), ConfigurationManager.AppSettings["orgUrl"]));
Console.WriteLine("本程序用于向指定邮箱发送邮件!");
if (needConfirm == "Y")
{
Console.WriteLine("当前处于需要确认才会继续的模式,若要继续请输入Y然后回车确认!");
inputKey = Console.ReadLine();
if (inputKey.ToUpper() == "Y")
{
RetrieveAlternateKeys(orgSvc,whoRsp.UserId);
}
else
{
Console.WriteLine("你选择了取消运行!");
}
}
else
{
RetrieveAlternateKeys(orgSvc,whoRsp.UserId);
}
}
Console.Write("程序运行完成,按任意键退出." + DateTime.Now.ToString());
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("程序运行出错:" + ex.Message + ex.StackTrace);
Console.ReadLine();
}
} private static void RetrieveAlternateKeys(OrganizationServiceProxy orgSvc,Guid userId)
{
const string functionName = "用于向指定邮箱发送邮件";
var EmailAddrs = ConfigurationManager.AppSettings["EmailAddrs"];
if (string.IsNullOrEmpty(EmailAddrs))
{
Console.WriteLine("收件邮箱列表为空!");
return;
}
Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString()));
try
{
Entity emailEntity = new Entity("email");
var newtoPartyList = new List<Entity>();
var fromEntity = new Entity("activityparty");
fromEntity["partyid"] = new EntityReference("systemuser", userId);
emailEntity["from"] = new Entity[] { fromEntity };
foreach (var item in EmailAddrs.Split(';'))
{
var toEntity = new Entity("activityparty");
toEntity["paricipationtypemask"] = new OptionSetValue();
toEntity["addressused"] = item;
newtoPartyList.Add(toEntity);
}
emailEntity["to"] = newtoPartyList.ToArray();
emailEntity["subject"] = ConfigurationManager.AppSettings["subject"];
emailEntity["description"] = ConfigurationManager.AppSettings["description"];
//emailEntity["regardingobjectid"] = new EntityReference("systemuser", userid);
Guid emailGuid = orgSvc.Create(emailEntity);
Console.WriteLine(string.Format("创建的邮件ID是{0}", emailGuid));
SendEmailRequest sendEmailRequest = new SendEmailRequest
{
EmailId = emailGuid,
TrackingToken = string.Empty,
IssueSend = true
};
orgSvc.Execute(sendEmailRequest);
Console.WriteLine("执行发送邮件消息成功!");
}
catch (Exception ex)
{
Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace));
}
Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString()));
Console.WriteLine("================================================");
} private static TProxy GetProxy<TService, TProxy>(
IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
where TService : class
where TProxy : ServiceProxy<TService>
{
Type classType = typeof(TProxy); if (serviceManagement.AuthenticationType !=
AuthenticationProviderType.ActiveDirectory)
{
AuthenticationCredentials tokenCredentials =
serviceManagement.Authenticate(authCredentials);
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
.Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
}
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
.Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
}
}
}

Dynamics 365使用代码发送邮件给指定邮箱地址的更多相关文章

  1. html实现邮箱发送邮件_js发送邮件至指定邮箱功能

    在前端开发中,JavaScript并没有提供直接操作Email邮箱的功能方法,但是遇到这样的需求,我们应该如何实现js发送邮件至指定邮箱功能呢?下面列举能够在通过前端实现邮件发送的几种方式: 方式一: ...

  2. Android 调用系统邮件,发送邮件到指定邮箱

    在项目中,最后有一个联络我们,要求是点击号码还有邮箱地址能够发送邮件,这时候解决的方案其实有两种,一种是调用系统发邮件的软件,可以添加邮箱账号就可以发送邮件:第二种是使用javamail来发送邮件.在 ...

  3. python (18)在linux中如何实现定时发送邮件到指定邮箱,监测任务

    最近要用到,定时发送邮件功能: 如何定时,当然要用到linux中crontab了 如下的代码能够定时发送邮件 #!/usr/bin/env python # -*- coding=utf-8 -*- ...

  4. Linux实战(17):Linux配置用户登陆时发送邮件到指定邮箱

    参考其他文章,正好有这个需求,记一笔做个记录,以防丢失. 参考链接 #!/bin/bash yum install -y mailx cat >> /etc/mail.rc<< ...

  5. 《JavaWeb从入门到改行》注册时向指定邮箱发送邮件激活

    javaMail API javaMail是SUN公司提供的针对邮件的API . 两个jar包  mail.jar 和 activation.jar java mail中主要类:javax.mail. ...

  6. Dynamics 365创建电子邮箱字段包含值的联系人同时更改负责人的方法。

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20171129可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  7. Dynamics 365客户端编程示例:获取当前用户的信息,表单级通知/提示,表单OnLoad事件执行代码

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  8. 通过C#代码调用Dynamics 365 Web API执行批量操作

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  9. 十、.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

    十..NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件 1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; us ...

随机推荐

  1. MongoDB 错误汇总

    错误1. ERROR: child process failed, exited with error number 100 可能原因: 1.没有正确关闭服务 2.服务已经启动 3.conf文件的参数 ...

  2. 【RL-TCPnet网络教程】第32章 RL-TCPnet之Telnet服务器

    第32章      RL-TCPnet之Telnet服务器 本章节为大家讲解RL-TCPnet的Telnet应用,学习本章节前,务必要优先学习第31章的Telnet基础知识.有了这些基础知识之后,再搞 ...

  3. [Swift]LeetCode46. 全排列 | Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  4. [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  5. FAutoTest-微信小程序 / 公众号H5 自动化利器

    X5内核H5自动化背景 近来有很多童靴咨询如何做微信小程序/公众号等H5页面来做自动化,之前写了一篇文章微信小程序自动化测试实践 https://www.cnblogs.com/yyoba/p/945 ...

  6. Jason Wang: 结对编程 CountWord(第三次作业)

    本次作业地址: https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/2882 学号: 201731072323 ...

  7. MySQL查询昨天的数据

    SELECT * FROM `表名` WHERE TO_DAYS(`时间字段名`) = TO_DAYS(NOW()) - 1; 需要前几天的话就在后面减几天.

  8. java多线程(8)---阻塞队列

    阻塞队列 再写阻塞列队之前,我写了一篇有关queue集合相关博客,也主要是为这篇做铺垫的. 网址:[java提高]---queue集合  在这篇博客中我们接触的队列都是非阻塞队列,比如Priority ...

  9. 解放双手—Cobbler批量自动化部署多版本系统

    1 Cobbler  介绍 Cobbler 是一个 Linux 服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理 DHCP,DNS 等.Cobble ...

  10. leetcode — validate-binary-search-tree

    import apple.laf.JRSUIUtils; /** * Source : https://oj.leetcode.com/problems/validate-binary-search- ...