使用MailKit中的结果剃刀视图发送电子邮件 [英] Sending email using resulted razor view in MailKit

查看:92
本文介绍了使用MailKit中的结果剃刀视图发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望我可以用一种简单的方式解释我的请求:

Hope I can explain my request in a simple way:

在.Net Core中,我们可以显示 .cshtml 视图,使用 View(FileName,Model)发送模型数据后。

In .Net Core, we can display the .cshtml view, after sending the model data using View(FileName, Model).

有没有办法将 Model 发送到 .cshtml 文件,以便与其显示结果视图,不给我发送电子邮件附件使用 Mailkit

Is there a way to send the Model to the .cshtml file, so that instead of displaying the resulting view I get it emailed a attachment using Mailkit

推荐答案

感谢巴黎Polyzos 和他的文章

我找到了解决方案,因此喜欢分享,也许有人可以

I found the solution, so liked to share it, may some one get a benefit from it, or improve it.

Firs:我们需要创建 service 来将Rasor转换为String ,代码 Razor2String.cs 如下:

Firs: We need to create service to convert the Rasor into String, the code Razor2String.cs is below:

using System
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
 
namespace Project.Utilities
{
    public interface IViewRenderService
    {
        Task<string> RenderToStringAsync(string viewName, object model);
    }
 
    public class ViewRenderService : IViewRenderService
    {
        private readonly IRazorViewEngine _razorViewEngine;
        private readonly ITempDataProvider _tempDataProvider;
        private readonly IServiceProvider _serviceProvider;
         public ViewRenderService(IRazorViewEngine razorViewEngine,
            ITempDataProvider tempDataProvider,
            IServiceProvider serviceProvider)
        {
            _razorViewEngine = razorViewEngine;
            _tempDataProvider = tempDataProvider;
            _serviceProvider = serviceProvider;
        }
 
        public async Task<string> RenderToStringAsync(string viewName, object model)
        {
            var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
 
            using (var sw = new StringWriter())
            {
                var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
                if (viewResult.View == null)
                {
                    throw new ArgumentNullException($"{viewName} does not match any available view");
                }
 
                var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = model
                };
 
                var viewContext = new ViewContext(
                    actionContext,
                    viewResult.View,
                    viewDictionary,
                    new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
                    sw,
                    new HtmlHelperOptions()
                );
 
                await viewResult.View.RenderAsync(viewContext);
                return sw.ToString();
            }
        }
    }
}

第二:我们需要将 preserveCompilationContext:true 添加到 buildOptions ,所以项目.json 变为:

Second: We need to add the "preserveCompilationContext": true to the buildOptions, so the project.json become:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "dependencies": {
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "MailKit":"1.10.0"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

第三:我们需要将服务添加到 ConfigureServices Startup 类中的$ c>,因此 Startup.cs 文件变为:

Third: We need to add the service to the ConfigureServices in the Startup class, so that the Startup.cs file become:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Project.Utilities;

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
             // Add application services.
            services.AddScoped<IViewRenderService, ViewRenderService>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync(
                    "Hello World of the last resort. The Time is: " +
                    DateTime.Now.ToString("hh:mm:ss tt"));
            });
        }
    }

第四:定义您的模型,类似于 users.cs

Fourth: Define your Model, something like users.cs:

namespace myApp
{
     public class users {
        public string   UserId {get; set;}
        public string   UserName {get; set;}
    } 
}

第五:创建 View 模板,在 Views 文件夹中,类似于 Views / Razor2String.cshtml

Fifth: Create the View template, in the Views folder, something like Views/Razor2String.cshtml:

Hello
<hr/>

@{
    ViewData["Title"] = "Contact";
}
<h2>@ViewData["Title"].</h2>
<h3>user id: @Model.UserId</h3>

第六: Program.cs 很简单

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace myApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseKestrel()
                .UseUrls("http://localhost:50000")
                .UseStartup<Startup>()
                .Build();
            host.Run();
        }
    }
}

第七名:主要部分, 'Email.cs':

Seventh: The main part, 'Email.cs':

using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;

using Project.Utilities;
using System.Threading.Tasks;

using MailKit.Net.Smtp;   // for SmtpClient
using MimeKit;  // for MimeMessage, MailboxAddress and MailboxAddress

namespace myApp
{
    [Route("api")]
    public class RenderController : Controller
    {
        private readonly IViewRenderService _viewRenderService;
        public RenderController(IViewRenderService viewRenderService)
        {
            _viewRenderService = viewRenderService;
        }
     
      [Route("sendEmail")]
      public async Task sendEmail()
        {
            var viewModel = new users
            {
                UserId = "cdb86aea-e3d6-4fdd-9b7f-55e12b710f78",
                UserName = "iggy",
            };
     
            // Get the generated Razor view as String
            var result = await _viewRenderService.RenderToStringAsync("Razor2String", viewModel);

            MemoryStream stream = new MemoryStream ();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write((String)result);
            writer.Flush();
            stream.Position = 0;

            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Hasan Yousef", "mySelf@gmail.com"));
            message.To.Add(new MailboxAddress("Personal", "myPersonalEmail@gmail.com"));
            message.Subject = "Email Test";
            var bodyBuilder = new BodyBuilder();

            bodyBuilder.HtmlBody = @"<div>HTML email body</Div>";

            bodyBuilder.Attachments.Add ("msg.html", stream);

            message.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
                {
                    client.Connect("smtp.gmail.com", 587);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");  // due to enabling less secure apps access
                Console.WriteLine("Prepairing the Email");
                    try{
                        client.Authenticate("mySelf@gmail.com", "myPSWD");
                        Console.WriteLine("Auth Completed");
                    }
                    catch (Exception e){
                    Console.WriteLine("ERROR Auth"); 
                    }
                    try{
                        client.Send(message);
                        Console.WriteLine("Email had been sent");
                    }
                    catch (Exception e){
                        Console.WriteLine("ERROR");
                    }
                    client.Disconnect(true);
                }
        }
    }
}

如果您需要将字符串发送回浏览器,可以使用:

If you need the string to be sent back for the browser, you can use:

  [Route("returnView")]
  public async Task<IActionResult> returnView()
    {
        var viewModel = new users
        {
            UserId = "cdb86aea-e3d6-4fdd-9b7f-55e12b710f78",
            UserName = "iggy",
        };
 
        // Get the generated Razor view as String
        var result = await _viewRenderService.RenderToStringAsync("Razor2String", viewModel);
        return Content(result);
    }

如果您需要将结果发送到AJAX请求,则可以使用类似:

If you need to send the result to AJAX request, you can use something like:

public async Task<String> RenderInviteView()
{
.
.
.
return result;
}

如果要使用单独的方法将字符串转换为流,例如 GenerateStreamFromString(result),然后您可以使用调用它(使用Stream流= GenerateStreamFromString(result)){}

If you want to have separate method to convert the string into stream like GenerateStreamFromString(result), then you can call it using using (Stream stream = GenerateStreamFromString(result)){ }

这篇关于使用MailKit中的结果剃刀视图发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆