• 用System.Net.Mail的命名空间发邮件


    难得项目空闲,用了点时间看了下.Net的Smtp类,略作随笔

    首先当然是引用程序集:

    System.Net
    System.Net.Mail

    然后需要实例化MailMessage对象:

    MailAddress from = new MailAddress("from@163.com");
    MailAddress to = new MailAddress("to@163.com"); 

    MailMessage message = new MailMessage(from, to);

    还有其他邮件属性,例如抄送, 暗送, 附件等, 详见MSDN

    由于抄送属性是只读的,所以要这么实现:

    message.CC.Add("CarbonCopies@163.com"); 

    再然后需要实例化Smtp对象:

    SmtpClient smtp = new SmtpClient("smtp.163.com", "25");

    这里需要用户验证:

    smtp.Credentials = new NetworkCredential("from@163.com", "Password");

    NetworkCredential属于System.Net命名空间内

    最后,就可以发送邮件啦~ 很简单
    try
    {
      smtp.Send(message);
      //Please output some information to make a easier understand for users
    }
    catch { do something.. }

  • 相关阅读:
    UESTC
    Education Round 8 A
    Gym
    Gym
    hdoj 1159 Common Subsequence
    UVA
    UESTC
    51Nod 1068 Bash游戏 V3 (这规律不好找)
    51Nod 1066 Bash游戏
    51Nod 1002 数塔取数问题
  • 原文地址:https://www.cnblogs.com/chenjunsheep/p/1728008.html
Copyright © 2020-2023  润新知