zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    

      

  • 相关阅读:
    八、springboot 简单优雅的通过docker-compose 构建
    【并发编程】ThreadLocal其实很简单
    计算机网络
    相似度
    不同激活函数的区别
    快速排序+归并排序
    xgboost
    c++面试
    PCA算法和SVD
    各种排序算法的时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576162.html
Copyright © 2011-2022 走看看