Dynamics 365使用代码发送邮件给指定邮箱地址
摘要: 微软动态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使用代码发送邮件给指定邮箱地址的更多相关文章
- html实现邮箱发送邮件_js发送邮件至指定邮箱功能
在前端开发中,JavaScript并没有提供直接操作Email邮箱的功能方法,但是遇到这样的需求,我们应该如何实现js发送邮件至指定邮箱功能呢?下面列举能够在通过前端实现邮件发送的几种方式: 方式一: ...
- Android 调用系统邮件,发送邮件到指定邮箱
在项目中,最后有一个联络我们,要求是点击号码还有邮箱地址能够发送邮件,这时候解决的方案其实有两种,一种是调用系统发邮件的软件,可以添加邮箱账号就可以发送邮件:第二种是使用javamail来发送邮件.在 ...
- python (18)在linux中如何实现定时发送邮件到指定邮箱,监测任务
最近要用到,定时发送邮件功能: 如何定时,当然要用到linux中crontab了 如下的代码能够定时发送邮件 #!/usr/bin/env python # -*- coding=utf-8 -*- ...
- Linux实战(17):Linux配置用户登陆时发送邮件到指定邮箱
参考其他文章,正好有这个需求,记一笔做个记录,以防丢失. 参考链接 #!/bin/bash yum install -y mailx cat >> /etc/mail.rc<< ...
- 《JavaWeb从入门到改行》注册时向指定邮箱发送邮件激活
javaMail API javaMail是SUN公司提供的针对邮件的API . 两个jar包 mail.jar 和 activation.jar java mail中主要类:javax.mail. ...
- Dynamics 365创建电子邮箱字段包含值的联系人同时更改负责人的方法。
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20171129可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...
- Dynamics 365客户端编程示例:获取当前用户的信息,表单级通知/提示,表单OnLoad事件执行代码
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 通过C#代码调用Dynamics 365 Web API执行批量操作
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 十、.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件
十..NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件 1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; us ...
随机推荐
- Java作业 十一(2017-11-13)
/*关键字*/ package com.baidu.www; abstract class A { private String name; public A(String name) { this. ...
- 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(4)
六.Libvirt 对于Libvirt,在启动虚拟机之前,首先需要define虚拟机,是一个XML格式的文件 列出所有的Instance # virsh list Id Name ...
- 原生javascript实现 下拉框搜索功能
由于业务需求,要实现 一个下拉框搜索功能.这个下拉功能和百度的还是有点区别的,百度的是时时与服务器交互的,而这个只是模拟.技术点在于实现 了搜索功能. 未搜索前如下图: 搜索后: <!DOCTY ...
- 【安富莱专题教程第4期】SEGGER的J-Scope波形上位机软件,HSS模式简单易用,无需额外资源,也不需要写目标板代码
说明:1.在实际项目中,很多时候,我们需要将传感器或者ADC的数值以波形的形式显示.通常的解决办法是用串口上位机,USB接口上位机或者MDK的逻辑分析仪功能,使用这三种方式都比较繁琐.本期专题为大家讲 ...
- 如何理解Python装饰器
如何理解Python装饰器?很多学员对此都有疑问,那么上海尚学堂python培训这篇文章就给予答复. 一.预备知识 首先要理解装饰器,首先要先理解在 Python 中很重要的一个概念就是:“函数是 F ...
- Web前端-JavaScript基础教程上
Web前端-JavaScript基础教程 将放入菜单栏中,便于阅读! JavaScript是web前端开发的编程语言,大多数网站都使用到了JavaScript,所以我们要进行学习,JavaScript ...
- [Swift]LeetCode605. 种花问题 | Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- [Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- "人机"对战:电脑太简单了,我是射手 skr~skr~skr
9月17日,2018 世界人工智能大会在上海拉开帷幕.在 SAIL 榜单入围项目中,我看到了小爱同学.小马智行.微软小冰.腾讯觅影等等,这不仅让我大开了眼界,也不禁让我感慨 AI 的发展神速.犹记得去 ...
- AutoFac - 将 autofac 应用于MVC多层项目
一.前言 AutoFac是.NET平台下的一款著名的IoC Container,它可以让我们很轻松的解除项目中服务类的接口与客户类的接口实现类之间的依赖关系,从而降低系统各模块之间耦合程度以提高系统的 ...