C#发送电子邮件

frombegintoend 原文 C#发送电子邮件

 

 

    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();  
        }  
    }  
}  

 

posted @ 2016-12-08 23:34  wenglabs  阅读(3152)  评论(0编辑  收藏  举报