[英]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.