简体   繁体   English

Mailkit asp.net 核心。 如何使用密码使其不显示在我的代码中

[英]Mailkit asp.net core. How to use password so it is not showing in my code

I can't seem to save my password elsewhere and use it in my code?我似乎无法将我的密码保存在其他地方并在我的代码中使用它? How would I go about doing this?我该怎么做呢? I am trying to save it in appsetting.json but I can't get it to send an email.我试图将它保存在 appsetting.json 中,但我无法让它发送电子邮件。 It only works when I hard code my credentials.它仅在我对凭据进行硬编码时才有效。

public IActionResult Contact(ContactViewModel contactViewModel)
{
if (ModelState.IsValid)
{
try
{
//instantiate a new MimeMessage 
var message = new MimeMessage();
//Setting the To e-mail address
message.To.Add(new MailboxAddress("E-mail Recipient Name", 
"randocommando2345@gmail.com"));
//Setting the From e-mail address
message.From.Add(new MailboxAddress("E-mail From Name", "from@domain.com"));
//E-mail subject 
message.Subject = contactViewModel.typeof_app;
//E-mail message body
message.Body = new TextPart(TextFormat.Html)
{
Text = "Here is what they need: " + contactViewModel.Comment + " <br /> Message was sent by: \n" + 
contactViewModel.first_name + contactViewModel.last_name + "<br /> E-mail: " + contactViewModel.email
};

//Configure the e-mail
using (var emailClient = new SmtpClient())
{
emailClient.Connect("smtp.gmail.com", 587, false);
emailClient.Authenticate("emailaccount", "password");
emailClient.Send(message);
emailClient.Disconnect(true);
}
}
catch (Exception ex)
{
ModelState.Clear();
ViewBag.Message = $" Crap! We have a problem here {ex.Message}";
}
}
return View();
}

You can get values from appsettigns.json by injecting IConfiguration file into your controller.您可以通过将IConfiguration文件注入控制器来从 appsettings.json 获取值。 My appsettings.json looks like this我的appsettings.json看起来像这样

{
"Logging": {
"LogLevel": {
  "Default": "Warning"
}
},
"AllowedHosts": "*",
"Hello": "Hi from appsettings",
"EmailSettings": {
"Email": "example@email.com",
"Password": "Password",
"Port": "5000"
 }
} 

And My controller look like this我的控制器看起来像这样

    private readonly IConfiguration _configuration;
    public HomeController(IConfiguration configuration)
    {           
        _configuration = configuration;
    }
    public IActionResult Index()
    {           
       var getWholeSection =  _configuration.GetSection("EmailSettings").GetChildren().ToList();
        var singleValue = _configuration.GetValue(typeof(string),"Hello");

        return View();
    }

Output:输出:

在此处输入图片说明

Second Method: Another Way you could wrap your section into as view model then use it then you have to modify your startup.cs class第二种方法:另一种方法,你可以将你的部分包装成视图模型,然后使用它,然后你必须修改你的startup.cs

services.Configure<MailSettignsViewModel>(Configuration.GetSection("EmailSettings"));

Where MailSettignsViewModel is my user defined class其中MailSettignsViewModel是我的用户定义类

public class MailSettignsViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
    public string Port { get; set; }
}

Then I have to inject IOptions with my view model like this into my controller然后我必须像这样将带有我的视图模型的IOptions注入到我的控制器中

private readonly IOptions<MailSettignsViewModel> options;
    public HomeController(IOptions<MailSettignsViewModel> _options)
    {
        options = _options;
    }
    public IActionResult Index()
    {
       var getSettigns =  options.Value;           
        return View();
    }

Output:输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心。 更改密码后如何使 JWT-Token 失效 - ASP.NET Core. How can I invalidate JWT-Token after password change ASP.NET核心。 如何与n层体系结构一起使用? - ASP.NET Core. How to use with n-tier architecture? 如何在 Asp.Net Core 中使用 MailKit 发送 email - how to send email using MailKit in Asp.Net Core IFormFile 空 ASP.NET Core。 我复制了一个工作代码,但复制的代码不起作用 - IFormFile empty ASP.NET Core. I've copied a working code but the replicated code is not working ASP.NET核心。 重置所有身份验证 - ASP.NET CORE. Reset all authentications ASP.NET 核心。 如何在没有身份用户帐户的情况下创建身份验证 cookie? - ASP.NET Core. How can i create an auth cookie without an identity user account? ASP.NET 内核中的 GraphQL 中的订阅。 如何切换到 SubscriptionDocumentExecuter - Subscriptions in GraphQL in ASP.NET Core. How do I switch to SubscriptionDocumentExecuter ASP.NET Core 3 传递电子邮件列表以发送方法 Mailkit - ASP.NET Core 3 Passing List of Emails to Send Method Mailkit 如何在asp.net core中使用Mailkit通过“与我们联系”表单发送电子邮件 - How to Send an email through “Contact us” Form using Mailkit in asp.net core 如何在 asp.net 内核中获取项目的根目录。 Directory.GetCurrentDirectory() 在 Mac 上似乎无法正常工作 - How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM