ASP.NET Core 1.1是一个跨平台的开源Web应用程序框架,用于构建高性能、可扩展的Web应用程序。下面是使用ASP.NET Core 1.1从Outlook发送电子邮件的步骤:
public class EmailService
{
private readonly SmtpSettings _smtpSettings;
public EmailService(IOptions<SmtpSettings> smtpSettings)
{
_smtpSettings = smtpSettings.Value;
}
public void SendEmail(string to, string subject, string body)
{
using (var client = new SmtpClient(_smtpSettings.Server, _smtpSettings.Port))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(_smtpSettings.Username, _smtpSettings.Password);
client.EnableSsl = true;
var message = new MailMessage(_smtpSettings.Username, to, subject, body);
client.Send(message);
}
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// 其他配置...
services.Configure<SmtpSettings>(Configuration.GetSection("SmtpSettings"));
services.AddTransient<EmailService>();
}
// 其他配置...
}
public class HomeController : Controller
{
private readonly EmailService _emailService;
public HomeController(EmailService emailService)
{
_emailService = emailService;
}
public IActionResult Index()
{
// 发送电子邮件
_emailService.SendEmail("recipient@example.com", "Hello", "This is a test email.");
return View();
}
}
这样,你就可以使用ASP.NET Core 1.1从Outlook发送电子邮件了。请注意,以上示例中的SMTP服务器配置信息需要根据实际情况进行修改。
领取专属 10元无门槛券
手把手带您无忧上云