具体的C#实现发送邮件的三种方法攻略如下:
一、使用SMTP客户端库发送邮件
步骤:
- 引用
System.Net.Mail
命名空间
csharp
using System.Net.Mail;
- 创建
SmtpClient
对象,并设置邮件服务器和端口号等相关信息
csharp
SmtpClient smtpClient = new SmtpClient("smtp.example.com", 25);
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
- 创建
MailMessage
对象,设置邮件的发送者、接收者、主题、正文等相关信息
csharp
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@example.com");
mailMessage.To.Add("recipient1@example.com");
mailMessage.To.Add("recipient2@example.com");
mailMessage.Subject = "C#实现发送邮件的三种方法";
mailMessage.Body = "这是一封测试邮件。";
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.Normal;
- 使用
SmtpClient
对象的Send
方法发送邮件
csharp
smtpClient.Send(mailMessage);
示例:
using System.Net;
using System.Net.Mail;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtpClient = new SmtpClient("smtp.example.com", 25);
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@example.com");
mailMessage.To.Add("recipient1@example.com");
mailMessage.To.Add("recipient2@example.com");
mailMessage.Subject = "C#实现发送邮件的三种方法";
mailMessage.Body = "这是一封测试邮件。";
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.Normal;
smtpClient.Send(mailMessage);
}
}
}
二、使用自带的MailMessage
类发送邮件
步骤:
- 引用
System.Web.Mail
命名空间
csharp
using System.Web.Mail;
- 创建
MailMessage
对象,并设置邮件的发送者、接收者、主题、正文等相关信息
csharp
MailMessage mailMessage = new MailMessage();
mailMessage.From = "sender@example.com";
mailMessage.To = "recipient1@example.com;recipient2@example.com";
mailMessage.Subject = "C#实现发送邮件的三种方法";
mailMessage.Body = "这是一封测试邮件。";
mailMessage.Priority = MailPriority.Normal;
- 使用
SmtpMail
类的Send
方法发送邮件
csharp
SmtpMail.SmtpServer = "smtp.example.com";
SmtpMail.Send(mailMessage);
示例:
using System.Web.Mail;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = "sender@example.com";
mailMessage.To = "recipient1@example.com;recipient2@example.com";
mailMessage.Subject = "C#实现发送邮件的三种方法";
mailMessage.Body = "这是一封测试邮件。";
mailMessage.Priority = MailPriority.Normal;
SmtpMail.SmtpServer = "smtp.example.com";
SmtpMail.Send(mailMessage);
}
}
}
三、使用自定义的SMTP客户端类发送邮件
步骤:
- 创建自定义的SMTP客户端类,继承自
SmtpClient
类,并重写Send
方法
```csharp
public class CustomSmtpClient : SmtpClient
{
public CustomSmtpClient(string host, int port, string username, string password)
{
this.Host = host;
this.Port = port;
this.Credentials = new NetworkCredential(username, password);
this.EnableSsl = true;
}
public void Send(string sender, string recipient, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(sender);
mailMessage.To.Add(recipient);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.Normal;
this.Send(mailMessage);
}
}
```
- 创建自定义的SMTP客户端对象,并调用
Send
方法发送邮件
csharp
CustomSmtpClient customSmtpClient = new CustomSmtpClient("smtp.example.com", 25, "username", "password");
customSmtpClient.Send("sender@example.com", "recipient@example.com", "C#实现发送邮件的三种方法", "这是一封测试邮件。");
示例:
using System.Net;
using System.Net.Mail;
namespace ConsoleApp1
{
public class CustomSmtpClient : SmtpClient
{
public CustomSmtpClient(string host, int port, string username, string password)
{
this.Host = host;
this.Port = port;
this.Credentials = new NetworkCredential(username, password);
this.EnableSsl = true;
}
public void Send(string sender, string recipient, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(sender);
mailMessage.To.Add(recipient);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.Normal;
this.Send(mailMessage);
}
}
class Program
{
static void Main(string[] args)
{
CustomSmtpClient customSmtpClient = new CustomSmtpClient("smtp.example.com", 25, "username", "password");
customSmtpClient.Send("sender@example.com", "recipient@example.com", "C#实现发送邮件的三种方法", "这是一封测试邮件。");
}
}
}
以上就是C#实现发送邮件的三种方法的完整攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现发送邮件的三种方法 - Python技术站