发送邮件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Net.Mail;

/// <summary>
/// SendEmail 的摘要说明
/// </summary>
public class SendEmail
{

    SqlDataAccess.Common.BasicDAL _bd = new SqlDataAccess.Common.BasicDAL();

    /// 发送短信
    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="addressto">收件人</param>
    /// <param name="title">发件标题</param>
    /// <param name="contents">发件内容</param>
    /// <param name="CSHTitle">初始化标题</param>
    /// <returns></returns>
    public string sendE_mail(string addressto, string title, string contents,string CSHTitle)
    {
        string EmailInfo = _bd.GetToTable("S_SysParameterConfig", "AND [Type]='10019'", "[Values]");
        if (EmailInfo != string.Empty)
        {
            string[] emails = EmailInfo.Split('|');
            string fwq = ""; //SMTP服务器:发件人所用邮箱的SMTP服务器
            string fjryhm = ""; //发件人用户邮箱
            string fjrmm = ""; //发件人邮箱密码
            string cshbt = ""; //初始化标题
            if (emails.Length >= 1 && emails[0].ToString() != string.Empty)
            {
                fwq = emails[0].ToString();
            }
            else
            {
                return "发送状态:发送失败;失败原因:没有正确配置发件人所用邮箱的SMTP服务器!";
            }
            if (emails.Length >= 2 && emails[1].ToString() != string.Empty)
            {
                fjryhm = emails[1].ToString();
            }
            else
            {
                return "发送状态:发送失败;失败原因:没有正确配置发件人所用邮箱账号!";
            }
            if (emails.Length >= 3 &&  emails[2].ToString() != string.Empty)
            {
                fjrmm = emails[2].ToString();
            }
            else
            {
                return "发送状态:发送失败;失败原因:发件人所用邮箱账号与密码不匹配!";
            }          
         
            SmtpClient smtp = new SmtpClient(fwq);//SMTP服务器:发件人所用邮箱的SMTP服务器
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new System.Net.NetworkCredential(fjryhm, fjrmm);//发件人所用邮箱的账号,密码
            //(有的不需要@以后的,有点则需要完整的邮箱名)
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            MailMessage message = new MailMessage();
            message.From = new MailAddress(fjryhm, CSHTitle);
          
            MailAddress add = new MailAddress(addressto);
            message.To.Add(add);
            if (message.To.Count == 0)
            {
                return "发送状态:发送失败;失败原因:没有选择要发送的数据!";
            }
            else
            {
                message.IsBodyHtml = true;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = title;
                message.Body = contents;
                try
                {
                    smtp.Send(message);
                }
                catch
                {
                    return "发送状态:发送失败;失败原因:没有选择要发送的数据!";
                }
            }
        }
        else
        {
            return "发送状态:发送失败;失败原因:没有正确配置发件人信息!";
        }

        return "发送成功!";
    }

}

你可能感兴趣的:(发送邮件)