zoukankan      html  css  js  c++  java
  • C#—发邮件方法

    前几天,有个系统需要增加一个发邮件的功能,于是写了一个方法,如下:

    View Code
     1 /// <summary>
    2 ///
    3 /// </summary>
    4 /// <param name='clientHost'>邮件服务器地址</param>
    5 /// <param name='emailAddress'>发件人邮箱地址</param>
    6 /// <param name='receiveAddress'>收件人邮箱地址</param>
    7 /// <param name='userName'>发件人邮箱用户名</param>
    8 /// <param name='password'>发件人邮箱密码</param>
    9 /// <param name="subject">邮件主题</param>
    10 /// <param name="body">邮件正文</param>
    11 private void SendEmail(string clientHost, string emailAddress, string receiveAddress,
    12 string userName, string password, string subject, string body)
    13 {
    14 MailMessage mail = new MailMessage();
    15 mail.From = new MailAddress(emailAddress);
    16 mail.To.Add(new MailAddress(receiveAddress));
    17 mail.Subject = subject;
    18 mail.Body = body;
    19 mail.IsBodyHtml = true;
    20 mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
    21
    22 SmtpClient client = new SmtpClient();
    23 client.Host = clientHost;
    24 client.Credentials = new NetworkCredential(userName, password);
    25 client.DeliveryMethod = SmtpDeliveryMethod.Network;
    26 try
    27 {
    28 client.Send(mail);
    29 }
    30 catch (Exception ex)
    31 {
    32 // Get ex.Message and do something
    33 }
    34 }

    此方法需要引用两个命名空间:

    1 using System.Net.Mail;
    2 using System.Net;



     

  • 相关阅读:
    [Contest on 2020.4.2] 影帝杯狂欢赛
    [BZOJ 3821] 玄学
    CodeForces 432D Prefixes and Suffixes
    CodeForces 17E Palisection
    CodeForces 665E Beautiful Subarrays
    BZOJ 2989 数列
    changeeksdja
    Jmeter学习——1
    LoadRunner监控Linux与Windows方法(经典)
    LR检查点小结
  • 原文地址:https://www.cnblogs.com/leolis/p/2284100.html
Copyright © 2011-2022 走看看