关闭

.Net发送邮件

标签: .net邮件smtp
261人阅读 评论(0) 收藏 举报
分类:

最近写的一个工具要用到给客户发送邮件。

/// <summary>
        /// 发送邮件:带抄送 
        /// </summary>
        /// <param name="md"></param>
        /// <param name="CCadmin"></param>
        /// <returns></returns>
        public string Sendmail_CC(MailData md, string CCadmin)
        {
            string strState = "";

            if (LoadConfig.config_is_sendmail == "1") //==1 不发送
            {
                return "-9999";
            }

            try
            {
                MailAddress from = new MailAddress(md.strFrom, md.strNiCheng);     //message.From.DisplayName 
                MailAddress to = new MailAddress(md.strTo);  //接收方邮箱地址
                MailMessage message = new MailMessage(from, to);  //邮件相关信息
                message.Subject = md.strSubject;

                if (!string.IsNullOrEmpty(md.strFuJian))
                {
                    Attachment acc = new Attachment(md.strFuJian);
                    message.Attachments.Add(acc);
                }

                if (!string.IsNullOrEmpty(md.strFuJianE))
                {
                    Attachment acc2 = new Attachment(md.strFuJianE);
                    message.Attachments.Add(acc2);
                }
                message.Body = md.body;

                MailAddress copy = new MailAddress(CCadmin);
                message.CC.Add(copy);

                SmtpClient client = new SmtpClient(md.strSmtpsever, 587);
                client.Credentials = new NetworkCredential(md.strSender, md.strPwd);//服务器使用的邮箱
                int port = 587;

                //端口
                if (int.TryParse(LoadConfig.config_Port, out port))
                {
                    client.Port = port;//
                }
                else
                {
                    client.Port = port;//
                }
                //是否加密
                if (LoadConfig.config_JiaMi == "true")
                {
                    client.EnableSsl = true;
                }
                else
                {
                    client.EnableSsl = false;
                }

                client.Send(message);
                Thread.Sleep(500);//防止邮件没有发送完 到时下次发送时候造成文件已经打开

                strState = "成功发送";
                message.Dispose();
            }
            catch (Exception ex)
            {
                strState = ex.Message;
                Form1.logHelp.WriteLog(LogStyle.mailE_offen, "发送邮件函数 Sendmail(MailData md) " + ex.Message + " " + md.strFuJian);
            }
            return strState;
        }


应用:

                MailData m_admin = new MailData();
                m_admin.strSmtpsever = LoadConfig.config_smtp;
                m_admin.strPwd = LoadConfig.config_FK_Key;
                m_admin.strSender = LoadConfig.config_FK_Sender; //smtp使用的邮箱
                m_admin.strFrom = LoadConfig.config_FK_From;//收件人看见的邮箱
                m_admin.strNiCheng = LoadConfig.config_Close_NiCheng;
                m_admin.strSubject = LoadConfig.config_Close_Subject;
                //m_admin.strTo = LoadConfig.config_Close_SendTo;//  "295152369@qq.com"; //"lgp411409@sohu.com";//
                m_admin.strFuJian = FileName;
                m_admin.body = "Please refer to the attachment.";
             string Send_Admin_State = sm.Sendmail(m_admin);

其中 

MailData:
    /// <summary>
    /// 发邮件使用的信息
    /// </summary>
    public class MailData : object
    {
        public String strSmtpsever = "";
        public String strTo = "";
        public String strFrom = "";//收件人看见的地址
        public String strSender = "";//SMTP使用的地址
        public String strPwd = "";
        public String strFuJian = "";
        public String strFuJianE = "";
        public String strNiCheng = "";
        public String strSubject = "";
        public string body = "无";
    }




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49348次
    • 积分:988
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:11篇
    • 译文:0篇
    • 评论:5条
    最新评论