一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net C#发送邮件实现(显示发件人名称,收件人名称)

时间:2022-06-25 08:18:31 编辑:袖梨 来源:一聚教程网


实例一 MailHelper

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace MyQuery.Utils
{
    ///


    /// 封装邮件处理
    /// by 贾世义 2011-6-3
    ///

    public static class MailHelper
    {
        private static string smtpHost = null;
        private static int smptPort = 25;
        private static bool smtpIsUserCredentials = false;
        private static string smtpCredentialAccount = null;
        private static string smtpCredentialPassword = null;
        ///
        /// 设置发送邮件参数
        ///

        /// smtp服务器地址或名称
        /// smtp服务端口 一般为25
        /// 是否需要认证
        /// 需要认证时的用户
        /// 需要认证时的用户的密码
        public static void SetParameters(string host, int port, bool isUserCredentials, string account, string password)
        {
            smtpHost = host;
            smptPort = port;
            smtpIsUserCredentials = isUserCredentials;
            smtpCredentialAccount = account;
            smtpCredentialPassword = password;
        }
        ///
        /// 设置发送邮件参数 取配置
        ///

        private static void setParameters()
        {
            if (String.IsNullOrEmpty(smtpHost))
            {
                smtpHost = WebHelper.GetAppConfig("SmtpHost");
                smptPort = DataHelper.GetIntValue(WebHelper.GetAppConfig("SmptPort"), 25);
                smtpIsUserCredentials = Constants.TRUE_ID.Equals(WebHelper.GetAppConfig("SmtpIsUserCredentials"));
                smtpCredentialAccount = WebHelper.GetAppConfig("SmtpCredentialAccount");
                smtpCredentialPassword = WebHelper.GetAppConfig("SmtpCredentialPassword");
            }
        }
        ///
        /// 发送邮件 发送邮件错误不会抛出异常
        ///

        /// 收件人
        /// 标题/主题
        /// 信件内容
        /// 发件人 空则取系统配置
        public static void SendMail(string receivers, string title, string content, string sender)
        {
            if (!String.IsNullOrEmpty(receivers))
            {
                //初始化参数
                setParameters();
                if (!String.IsNullOrEmpty(smtpHost))
                {
                    try
                    {
                        SmtpClient smtp = new SmtpClient(smtpHost, smptPort);
                        if (smtpIsUserCredentials)
                        {
                            smtp.UseDefaultCredentials = true;
                            smtp.Credentials = new NetworkCredential(smtpCredentialAccount, smtpCredentialPassword); ;
                        }
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        if (String.IsNullOrEmpty(sender))
                        {
                            sender = smtpCredentialAccount;
                        }
                        foreach (string receiver in DataHelper.GetStrings(receivers))
                        {
                            MailMessage msg = new MailMessage(sender, receiver, title, content);
                            msg.BodyEncoding = Encoding.UTF8;
                            msg.SubjectEncoding = Encoding.UTF8;
                            msg.IsBodyHtml = true;
                            smtp.Send(msg);
                            msg.Dispose();
                        }
                    }
                    catch { }
                }
            }
        }
    }
}

实例二SmtpClient

 

 代码如下 复制代码
    //创建smtpclient对象  
    System.Net.Mail.SmtpClient client = new SmtpClient();  
    client.Host = "smtp.163.com";//163的smtp服务器是 smtp.163.com  
    string from = "***@163.com";  
    string pwd = "密码";  
    string tomail = "***@qq.com";  
     
    client.UseDefaultCredentials = false;  
    client.Credentials = new System.Net.NetworkCredential(from, pwd);  
     
    client.DeliveryMethod = SmtpDeliveryMethod.Network;  
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;  
    string senderDisplayName = ConfigurationManager.AppSettings["senderDisplayName"];//这个配置的是发件人的要显示在邮件的名称  
    string recipientsDisplayName = "昊云";//这个配置的是收件人的要显示在邮件的名称  
     
    MailAddress mailfrom = new MailAddress(from, senderDisplayName, encoding);//发件人邮箱地址,名称,编码UTF8  
    MailAddress mailto = new MailAddress(tomail, recipientsDisplayName, encoding);//收件人邮箱地址,名称,编码UTF8  
    //创建mailMessage对象  
    System.Net.Mail.MailMessage message = new MailMessage(mailfrom, mailto);  
    message.Subject = "测试邮件标题";  
    //正文默认格式为html  
    message.Body = "测试邮件内容";  
    message.IsBodyHtml = true;  
    message.BodyEncoding = encoding;  
    message.SubjectEncoding = encoding;  
    message.HeadersEncoding = encoding;  
     
    client.Send(message); 

热门栏目