要使用System.Net.Mail为电子邮件添加附件,您需要遵循以下步骤:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@example.com");
mail.To.Add("recipient@example.com");
mail.Subject = "邮件主题";
mail.Body = "邮件正文";
Attachment attachment = new Attachment("path/to/file.ext");
mail.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("smtp.example.com");
smtp.Credentials = new NetworkCredential("username", "password");
smtp.Send(mail);
完整的示例代码如下:
using System.Net.Mail;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@example.com");
mail.To.Add("recipient@example.com");
mail.Subject = "邮件主题";
mail.Body = "邮件正文";
Attachment attachment = new Attachment("path/to/file.ext");
mail.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("smtp.example.com");
smtp.Credentials = new NetworkCredential("username", "password");
smtp.Send(mail);
请注意,这个示例代码仅适用于.NET Framework和.NET Core。如果您使用的是其他编程语言或框架,请参考相应的文档来实现电子邮件附件的添加。
领取专属 10元无门槛券
手把手带您无忧上云