using System;  
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Net;

namespace _11发送电子邮件
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("用户名");
string uid = Console.ReadLine();//发件人邮箱地址@符号前面的字符tom@dddd.com,则为"tom"
Console.WriteLine("密码");//发件人邮箱的密码
string pwd = Console.ReadLine();
for (int i = 0; i < 20; i++) //连发20封,嘿嘿
{
MailAddress from = new MailAddress("发件人的邮箱地址");
MailAddress to = new MailAddress("收件人的邮箱地址");
MailMessage mailMessage = new MailMessage(from, to);
mailMessage.Subject = "Test";//邮件主题
mailMessage.Body = "Hello, world!!";//邮件正文

//添加附件

string file1 = "test.txt";
Attachment attachment1 = new Attachment(file1, MediaTypeNames.Text.Plain);
//为附件天剑时间信息
ContentDisposition disposition1 = attachment1.ContentDisposition;
disposition1.CreationDate = File.GetCreationTime(file1);
disposition1.ModificationDate = File.GetLastWriteTime(file1);
disposition1.ReadDate = File.GetLastAccessTime(file1);
mailMessage.Attachments.Add(attachment1);

string file2 = "test.doc";
Attachment attachment2 = new Attachment(file2);
//为附件添加时间信息
ContentDisposition disposition2 = attachment2.ContentDisposition;
disposition2.CreationDate = File.GetCreationTime(file2);
disposition2.ModificationDate = File.GetLastWriteTime(file2);
disposition2.ReadDate = File.GetLastAccessTime(file2);
mailMessage.Attachments.Add(attachment2);

//实例化SmtpClient
SmtpClient smtpClient = new SmtpClient("smtp.yeah.net", 25);
//设置验证发件人身份的凭据
smtpClient.Credentials = new NetworkCredential(uid, pwd);
//发送
smtpClient.Send(mailMessage);

Console.WriteLine("OK - [{0}]",i);
}

Console.ReadKey();
}
}
}


 


你们的评论、反馈,及对你们有所用,是我整理材料和博文写作的最大的鼓励和唯一动力。欢迎讨论和关注!

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。