Sending mail from AX using .NET Framework.

From: http://blogs.msdn.com/czdaxsup/archive/2008/12/03/howto-sending-mail-from-ax-using-net-framework.aspx

 

Sometimes happen that SysMailer class (using CDO) is not the right solution for sending mails with attachments. There is a little sample of X++ Job that is using System.Net.Mail namespace to achieve same.

 

static void JobNETSendMail(Args _args)
{
    System.Net.Mail.MailMessage             mailMessage;
    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;
    System.Net.Mail.SmtpClient              smtpClient;
    System.Net.Mail.MailAddress             mailAddressFrom;
    System.Net.Mail.MailAddress             mailAddressTo;
    str                                     strBody;
    str                                     strSMTPServer;
    str                                     strFileName;
    FileIOPermission                        perm;
    ;

    // preparing parameters
    mailAddressFrom = new  System.Net.Mail.MailAddress("test@localmail.com", "");
    mailAddressTo = new  System.Net.Mail.MailAddress("admin@localmail.com","");
    strBody = "There is a email body";
    strSMTPServer = "MailServerName";
   
    // preparing mail with body, subject, from, to.
    mailMessage = new System.Net.Mail.MailMessage(mailAddressFrom, mailAddressTo);
    mailmessage.set_Subject("There is a email subject");
    mailmessage.set_Body(strBody);
    attachementCollection = mailMessage.get_Attachments();

    strFileName = "C://path//filename";
    // assert permision
    perm = new FileIOPermission(strFileName,'w');
    perm.assert();

    // attaching file to that email.
    attachment = new System.Net.Mail.Attachment(strFileName);
    attachementCollection.Add(attachment);
    smtpClient = new System.Net.Mail.SmtpClient(strSMTPServer);
    smtpClient.Send(mailmessage);
   
    // release permision
    CodeAccessPermission::revertAssert();
}

在C#中,如果你想要使用.NET Framework 4.5发送HTTPS GET请求并带Header参数,你可以使用HttpClient类。以下是一个示例: ```csharp using System; using System.Net.Http; using System.Net.Http.Headers; namespace YourNamespace { class Program { static void Main(string[] args) { // 创建HttpClient实例 using (HttpClient client = new HttpClient()) { // 设置请求的URL string url = "https://example.com/api/resource"; // 定义Header var headers = new Dictionary<string, string> { { "Content-Type", "application/json" }, { "Authorization", "Bearer your_token_here" } // 示例头,替换为你实际需要的值 }; // 使用UriBuilder构建完整的URL,如果包含查询字符串 if (!string.IsNullOrEmpty(url.Contains("?"))) { url += UriQueryBuilder.EncodeQuery(headers); } // 创建请求消息 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); // 添加Header到请求中 foreach (KeyValuePair<string, string> header in headers) { request.Headers.Add(header.Key, header.Value); } try { // 发送GET请求 HttpResponseMessage response = await client.SendAsync(request); // 检查响应状态码 if (response.IsSuccessStatusCode) { Console.WriteLine(await response.Content.ReadAsStringAsync()); } else { Console.WriteLine($"Error: {response.StatusCode}"); } } catch (Exception ex) { Console.WriteLine($"Error sending request: {ex.Message}"); } } } // 将Header添加到URL的查询字符串中,用于处理复杂情况 private static string UriQueryBuilderEncodeQuery(Dictionary<string, string> headers) { StringBuilder sb = new StringBuilder(); bool first = true; foreach (KeyValuePair<string, string> header in headers) { if (first) first = false; else sb.Append("&"); sb.AppendFormat("{0}={1}", Uri.EscapeDataString(header.Key), Uri.EscapeDataString(header.Value)); } return sb.ToString(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值