• C# 发送邮件


    1、首先添加引入命名空间:  using System.Net.Mail;

    2、开始写代码,如下:

     1         /// <summary>
     2         /// 邮件发送
     3         /// </summary>
     4         public JsonResult SendEmail(string Title,string Address)
     5         {
     6             bool result = false; 
     7             string add = "1115924081@QQ.com";
     8             string CC = "1115924081@QQ.com";
     9             string File = Server.MapPath("~/Images/1.jpg");
    10             string user = "15866554223@QQ.Com";
    11             string password = "123456";    
    12             SendEmail(Title, add, CC, File, user, password);
    13             return Json(result, JsonRequestBehavior.AllowGet);
    14         }
    15         /// <summary>
    16         /// 邮件发送
    17        /// </summary>
    18         /// <param name="Title">邮件标题</param>
    19         /// <param name="Address">要发送的地址</param>
    20         /// <param name="CCTo">抄送人</param>
    21         /// <param name="toFile">附件</param>
    22         /// <param name="UserName">邮箱账户</param>
    23         /// <param name="Password">邮箱密码</param>
    24         public void SendEmail(string Title, string Address, string CCTo,string toFile ,string UserName, string Password)
    25         {
    26             try
    27             {
    28                 //邮件发送类
    29                 MailMessage msg = new MailMessage();
    30                 //发送的邮箱  可以多个
    31                 msg.To.Add(Address);
    32                 //抄送人  可以多个
    33                 msg.CC.Add(CCTo);
    34                 //附件  可以多个
    35                 msg.Attachments.Add(new Attachment(toFile));
    36                 //三个参数依次是(显示的地址,显示名称,编码)
    37                 msg.From = new MailAddress("840967210@QQ.Com", "我的邮件", System.Text.Encoding.UTF8);
    38                 //标题
    39                 msg.Subject = Title;
    40                 //标题编码
    41                 msg.SubjectEncoding = System.Text.Encoding.UTF8;
    42                 //邮件内容
    43                 msg.Body = "………………测试邮件";
    44                 //邮件内容编码
    45                 msg.BodyEncoding = System.Text.Encoding.UTF8;
    46                 //是否支持Html
    47                 msg.IsBodyHtml = false;
    48                 //优先级
    49                 msg.Priority = MailPriority.High;
    50                 //发送邮件初始化
    51                 SmtpClient client = new SmtpClient();
    52                 //用户名和密码
    53                 client.Credentials = new System.Net.NetworkCredential(UserName, Password);
    54                 //端口号
    55                 client.Port = 465;//或 587
    56                 //主机名
    57                 client.Host = "smtp.qq.com";//接收:imap.qq.com  发送: smtp.qq.com
    58                 //发送
    59                 client.Send(msg);
    60             }
    61             catch (System.Net.Mail.SmtpException ex)
    62             {
    63                 throw ex;
    64             } 
    65         }
  • 相关阅读:
    Cognitive Radio Cognitive Network Simulator (NS3 based)
    RCU(Read-Copy Update)synchronize原理分析
    英雄黑客:一个试图提高物联网安全性的“义务警员”
    Android NDK放弃GCC,全面转向Clang
    Why is FreeBSD deprecating GCC in favor of Clang/LLVM?
    (Low Level Virtual Machine) LLVM 与 Clang介绍
    RMS谈GCC、LLVM和Copyleft
    LLVM和Clang背后的故事
    FEP: 测试 lineage, 获得 CPU, MEMORY 统计信息
    以wifi-example-sim.cc为例说明NS3统计数据模型
  • 原文地址:https://www.cnblogs.com/wwj1992/p/5128769.html
Copyright © 2020-2023  润新知