码迷,mamicode.com
首页 > Windows程序 > 详细

C# 邮件发送

时间:2018-10-10 17:08:28      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:resources   发送   配置   处理   名称   for   子邮件   ext   alter   

        /// <summary>
        /// 发送邮件的方法
        /// </summary>
        /// <param name="ServPath">Smtp事务的主机的名称或者ip地址</param>
        /// <param name="SendMail">发件人Email</param>
        /// <param name="sendPwd">发件人邮箱密码</param>
        /// <param name="SendName">发件人显示名称</param>
        /// <param name="CollectionMail">收件人Email</param>
        /// <param name="CollectionName">收件人名称</param>
        /// <param name="Subject">邮件主题</param>
        /// <param name="ListhtmlBody">插入html文本</param>
        /// <returns>发送邮件是否有失败或者异常</returns>
        public string MailSendhtml(string ServPath, string SendMail,string sendPwd,string SendName, string CollectionMail,string CollectionName,
            string Subject,List<AlternateView>ListhtmlBody)
        {
            string msg = "";
            try
            {
                //-----------------------------------------------------------------------
                //邮件服务器如下配置
                //确定smtp服务器端的地址,实列化一个客户端smtp 
                System.Net.Mail.SmtpClient sendSmtpClient = new System.Net.Mail.SmtpClient(ServPath);//发件人的邮件服务器地址
                //设置邮件信息 (指定如何处理待发的电子邮件)
                sendSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发邮件 是以网络来发
                sendSmtpClient.EnableSsl = false;//服务器支持安全接连,安全则为true
                sendSmtpClient.UseDefaultCredentials = false;//是否随着请求一起发
                //-----------------------------------------------------------------------
                //发件人地址配置
                System.Net.Mail.MailAddress sendMailAddress = new MailAddress(SendMail, SendName, Encoding.UTF8);
                //收件人地址配置
                System.Net.Mail.MailAddress consigneeMailAddress = new MailAddress(CollectionMail, CollectionName, Encoding.UTF8);
                //-----------------------------------------------------------------------
                //邮件信息配置
                System.Net.Mail.MailMessage mailMessage = new MailMessage(sendMailAddress, consigneeMailAddress);//发件地址和收件地址
                mailMessage.Subject = Subject;//邮件的主题  

                for (int i = 0; i < ListhtmlBody.Count; i++)
                {
                    mailMessage.AlternateViews.Add(ListhtmlBody[i]);
                }
                //string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
                //mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
                ////-----------------------------------------------------------------------
                ////HTML格式邮件的内容   
                //string htmlBodyContent = "以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>";
                //htmlBodyContent += "<img src=\"cid:weblogo\"  height=\"280px\" width=\"280px\">";   //注意此处嵌入的图片资源   
                //AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
                ////-------------------------------------------------------------------
                //LinkedResource lrImage = new LinkedResource(@"d:\baken.png", "image/gif");
                //lrImage.ContentId = "weblogo"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片   
                //htmlBody.LinkedResources.Add(lrImage);
                //mailMessage.AlternateViews.Add(htmlBody);
                ////-----------------------------------------------------------------------
                //string htmlbodycontent2 = "<h1>test12阿嘎如果十分不好温柔提交_搜狗知识(全部约100条结果) </h1>";
                //AlternateView htmlBody2 = AlternateView.CreateAlternateViewFromString(htmlbodycontent2, null, "text/html");
                //mailMessage.AlternateViews.Add(htmlBody2); 
                
                //用户登录信息
                NetworkCredential myCredential = new NetworkCredential(SendMail, sendPwd);
                sendSmtpClient.Credentials = myCredential;//登录
                sendSmtpClient.Send(mailMessage);//发邮件
                //return true;//发送成功
            }
            catch (Exception ex)
            {
                msg = ex.Message.ToString(); 
            }
            return msg;
        }

 

C# 邮件发送

标签:resources   发送   配置   处理   名称   for   子邮件   ext   alter   

原文地址:https://www.cnblogs.com/lanyubaicl/p/9767148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!