C#发送电子邮件代码记录

    /// 
    /// 发送电子邮件
    /// 
    /// 邮件地址
    /// 邮件标题
    /// 邮件内容
    private void SendEmail(string Address, string Subject, string Body)
    {
        string smtp = "smtp.163.com";
        if (!string.IsNullOrEmpty(smtp))
        {
            string from = "系统管理员";
            string userName = "[email protected]";// from;// @"coli\coli_workflow";
            string password = "xxxxx";

            try
            {
                // 发送该邮件
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(userName, password);
                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                client.EnableSsl = false;
                client.Port = 25;// VesselCustomSetting.GetValue(this.GetSettingValue(VesselCustomSetting.Setting_SmtpPort), 25);
                Encoding subjectEncoding = null;
                // 默认值是UTF8
                subjectEncoding = System.Text.Encoding.UTF8;

                Encoding bodyEncoding = subjectEncoding;

                this.SendMailBySmtp(client,
                    userName,
                    from,
                    subjectEncoding,
                    bodyEncoding,
                    Address,
                    Subject,
                    Body);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
    /// 
    /// 发送邮件
    /// 
    /// SMTP对象
    /// 发送的邮箱账号
    /// 发送的邮箱显示名称
    /// 编码
    /// 编码
    /// 接收邮箱的地址
    /// 邮件标题
    /// 邮件内容
    private void SendMailBySmtp(System.Net.Mail.SmtpClient SmtpClient,
        string UserName,
        string From,
        Encoding SubjectEncoding,
        Encoding BodyEncoding,
        string Address,
        string Title,
        string Content)
    {
        if (string.IsNullOrEmpty(Address))
        {
            return;
        }
        MailAddress fromAddress = new MailAddress(UserName, From, SubjectEncoding);
        MailAddress toAddress = new MailAddress(Address, Address, SubjectEncoding);
        MailMessage message = new MailMessage(fromAddress, toAddress)
        {
            Subject = Title,
            Body = Content,
            SubjectEncoding = SubjectEncoding,
            BodyEncoding = BodyEncoding,
            IsBodyHtml = true
        };

        SmtpClient.Send(message);
    }

 

你可能感兴趣的:(C#发送电子邮件代码记录)