• C# 发送邮件方法2


    一、 发送邮件程序:(使用System.Web.Mail下的类)

    复制代码
    public static bool SendMail(string mailhead, string mailbody, string emailadd)
    ...{
    if (emailadd != "")
    ...{
    //创建MailMessage对象
    System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
    //设置收件人的邮件地址
    mailMsg.To = emailadd;
    //设置发送者的邮件地址
    mailMsg.From = "wicresoft@126.com";
    //设置邮件主题
    mailMsg.Subject = mailhead;
    //设置邮件内容
    mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;

    if (mailbody != "")
    ...{
    mailMsg.Body
    = mailbody;

    //设置支持服务器验证
    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
    //设置用户名
    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "wicresoft");
    //设置用户密码
    mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "wangwei");
    try
    ...{
    //设置发送邮件服务器 202.108.5.142
    System.Web.Mail.SmtpMail.SmtpServer = "202.108.5.142";
    //发送邮件
    System.Web.Mail.SmtpMail.Send(mailMsg);
    return true;
    }
    catch
    ...{
    return false;
    }
    }
    else
    return false;
    }
    else
    return false;
    }


    本文来自CSDN博客,转载请标明出处:http:
    //blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx
    复制代码

     

    二、Net2.0又提供了一个替代方法(使用System.Nw)

     

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Net.Mail;

    namespace MailSender
    ...{
      
    class Program
      ...{
        
    static string strHost = string.Empty;
        
    static string strAccount = string.Empty;
        
    static string strPwd = string.Empty;
        
    static string strFrom = string.Empty;

        
    static void Main(string[] args)
        ...{
          strHost
    = "smtp.163.com";  //STMP服务器地址
          strAccount = "jailu";    //SMTP服务帐号
          strPwd = "123456789";    //SMTP服务密码
          strFrom = "jailu@163.com"; //发送方邮件地址

          Console.WriteLine(sendMail(
    "jailu@qq.com", "这是一封测试邮件", "这是一封测试邮件的正文内容") ? "Success" : "Unsuccess");

          Console.ReadLine();
        }

        
    /**//// <summary>
        
    /// 发送邮件
        
    /// </summary>
        
    /// <param name="to">接收方邮件地址</param>
        
    /// <param name="title">邮件标题</param>
        
    /// <param name="content">邮件正文内容</param>
        
    /// <returns></returns>
        static bool sendMail(string to, string title, string content)
        ...{
          SmtpClient smtpClient
    = new SmtpClient();
          smtpClient.DeliveryMethod
    = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
          smtpClient.Host = strHost; ;//指定SMTP服务器
          smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码

          MailMessage mailMessage
    = new MailMessage(strFrom,to);
          mailMessage.Subject
    = title;//主题
          mailMessage.Body = content;//内容
          mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
          mailMessage.IsBodyHtml = true;//设置为HTML格式
          mailMessage.Priority = MailPriority.High;//优先级

          
    try
          ...{
            smtpClient.Send(mailMessage);
            
    return true;
          }
          
    catch
          ...{
            
    return false;
          }
        }
      }
    }
  • 相关阅读:
    Awstats显示国家地区插件GeoIP安装 枯木
    springboot_shiro与shiro.ini文件
    Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
    mybatis的dao层xml
    springboot的配置文件
    万事无忧之用泡MM的方式演绎设计模式
    C#设计模式
    软件架构
    什么时候应该使用Web Service
    Spring框架快速入门之简介
  • 原文地址:https://www.cnblogs.com/dullbaby/p/5337230.html
Copyright © 2020-2023  润新知