在C#中将CSV作为电子邮件附件发送,可以通过以下步骤实现:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@example.com");
mail.To.Add("recipient@example.com");
mail.Subject = "CSV文件作为附件";
Attachment attachment = new Attachment("path/to/csv/file.csv");
mail.Attachments.Add(attachment);
mail.Body = "这是一封包含CSV文件附件的邮件,请查收。";
mail.IsBodyHtml = false; // 如果使用纯文本格式,设置为false;如果使用HTML格式,设置为true。
SmtpClient smtpClient = new SmtpClient("smtp.example.com", 587);
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Send(mail);
完整的代码示例:
using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main()
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@example.com");
mail.To.Add("recipient@example.com");
mail.Subject = "CSV文件作为附件";
Attachment attachment = new Attachment("path/to/csv/file.csv");
mail.Attachments.Add(attachment);
mail.Body = "这是一封包含CSV文件附件的邮件,请查收。";
mail.IsBodyHtml = false;
SmtpClient smtpClient = new SmtpClient("smtp.example.com", 587);
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Send(mail);
Console.WriteLine("邮件发送成功!");
}
catch (Exception ex)
{
Console.WriteLine("邮件发送失败:" + ex.Message);
}
}
}
在上述代码中,需要将以下内容替换为实际的信息:
注意:为了发送邮件成功,确保SMTP服务器的设置正确,并且发件人邮箱有权限使用该SMTP服务器发送邮件。
推荐的腾讯云相关产品:腾讯云邮件推送(https://cloud.tencent.com/product/ses)
领取专属 10元无门槛券
手把手带您无忧上云