码迷,mamicode.com
首页 > Windows程序 > 详细

C#发送电子邮件

时间:2016-12-09 08:05:00      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:实例化   new   添加   chm   time   符号   code   collect   subject   

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

 

C#发送电子邮件

标签:实例化   new   添加   chm   time   符号   code   collect   subject   

原文地址:http://www.cnblogs.com/arxive/p/6147806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!