以编程方式将文本文件的内容发送到电子邮件地址
本文关键字:电子邮件地址 方式 编程 文本 文件 | 更新日期: 2023-09-27 18:03:14
在我的web项目中,我试图以编程方式将项目中存在的文本文件的内容发送到默认电子邮件地址。在c#中有什么简单的方法吗?
类似于
// Read the file
string body = File.ReadAllText(@"C:''MyPath''file.txt");
MailMessage mail = new MailMessage("you@you.com", "them@them.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "file";
// Set the read file as the body of the message
mail.Body = body;
// Send the email
client.Send(mail);
假设你的文件是/files/file1.txt
所以要阅读它使用:
var content = System.IO.File.ReadAllText(Server.MapPath("/files/file1.txt"));
和发送
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");
message.To.Add(new MailAddress("the target email address"));
message.Subject = "...";
message.Body = content;
var client = new SmtpClient();
client.Send(message);
这里有一个例子:
MailMessage message = new MailMessage();
message.From = new MailAddress("from@from.be");
message.To.Add(new MailAddress("to@to.be"));
message.Subject = "Subject goes here.";
message.Body = File.ReadAllText("Path-to-file");
SmtpClient client = new SmtpClient();
client.Send(message);
您应该在构建电子邮件之外阅读邮件,但这里只是显示对文件的阅读。
Kr,