.net core之邮件发送怎么实现

   2025-02-21 9900
核心提示:在.NET Core中发送邮件可以使用System.Net.Mail.SmtpClient类。以下是一个示例代码来发送邮件:using System;using System.Net;u

在.NET Core中发送邮件可以使用System.Net.Mail.SmtpClient类。以下是一个示例代码来发送邮件:

using System;using System.Net;using System.Net.Mail;class Program{static void Main(){// 发件人邮箱地址string from = "sender@example.com";// 发件人邮箱密码(或授权码)string password = "password";// 收件人邮箱地址string to = "recipient@example.com";// 邮件主题string subject = "Hello from .NET Core";// 邮件内容string body = "This is a test email.";// 创建一个SmtpClient对象SmtpClient client = new SmtpClient("smtp.example.com", 587);// 设置使用的SMTP服务器地址和端口号client.EnableSsl = true; // 如果SMTP服务器要求SSL连接,请设置为true// 设置发件人邮箱地址和密码client.Credentials = new NetworkCredential(from, password);// 创建一个MailMessage对象MailMessage message = new MailMessage(from, to, subject, body);try{// 发送邮件client.Send(message);Console.WriteLine("Email sent successfully!");}catch (Exception ex){Console.WriteLine("Failed to send email: " + ex.Message);}}}

通过设置frompasswordtosubjectbody变量的值,你可以发送邮件。请确保将smtp.example.com更改为你的SMTP服务器地址,并根据需要设置其他SMTP服务器的配置。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言