首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ASP.NET & C# 发邮件的有关问题

2012-04-09 
ASP.NET & C# 发邮件的问题平时经常会收到一些邮件,打开之后就直接是一个图文并茂的网页;我想问一下这个功

ASP.NET & C# 发邮件的问题
平时经常会收到一些邮件,打开之后就直接是一个图文并茂的网页;我想问一下这个功能怎么实现?

我现在可以用MailMessage类实现发邮件的功能,但是只会发text页面;我知道发html的页面需要将MailMessage的属性IsBodyHtml设为true; 但是body部分应该怎么写呢? 



[解决办法]
参考:
http://www.cnblogs.com/insus/articles/1689279.html
[解决办法]
test.body="<html><body>.....</body></html>";
[解决办法]

多种发邮件的方法,同步,异步。。(要发送HTML格式的,把HTML代码拼接好,赋给Body就可以)

http://www.cnblogs.com/aijun/archive/2011/03/15/1984558.html
[解决办法]

C# code
#region 发送邮件        /// <summary>        /// 通过附件发送邮件        /// </summary>        /// <param name="emailFrom">发送人邮箱地址</param>        /// <param name="files">图片地址</param>        /// <param name="emailFromName">发送人姓名</param>        /// <param name="zsrList">收件人集合</param>        /// <param name="csrList">抄送人集合</param>        /// <param name="arrFilePath">附件路径</param>        /// <param name="mailSubject">主题</param>        /// <param name="mailBody">正文内容</param>        /// <returns></returns>        public static bool SendEmail(string emailFrom, string[] files, string emailFromName, IList<CSR> zsrList, IList<CSR> csrList, ArrayList arrFilePath, string mailSubject, string mailBody)        {            MailMessage Email = new MailMessage();            try            {                MailAddress EmailFrom = new MailAddress(emailFrom, emailFromName); //创建发件人邮箱地址对象                Email.From = EmailFrom; //指定发件人                Email.CC.Add(EmailFrom); //将发件人作为抄送人发一份                for (int i = 0; i < zsrList.Count; i++) //循环添加主送人邮箱地址对象                {                    MailAddress zsrAddr = new MailAddress(zsrList[i].EMAIL, zsrList[i].NAME);                    Email.To.Add(zsrAddr);                }                for (int i = 0; i < csrList.Count; i++) //循环添加抄送人邮箱地址对象                {                    MailAddress csrAddr = new MailAddress(csrList[i].EMAIL, csrList[i].NAME);                    Email.CC.Add(csrAddr);                }            }            catch            {                return false;            }            Email.IsBodyHtml = true;            Email.BodyEncoding = System.Text.Encoding.UTF8;            Email.Body += mailBody;            for (int i = 0; i < files.Length; i++)            {                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(files[i]);                Email.Attachments.Add(attachment);                Email.Body += "<img src=\"cid:" + attachment.ContentId + "\"/>";            }            Email.Priority = MailPriority.High;            //邮件主题            Email.Subject = mailSubject;            Email.SubjectEncoding = Encoding.GetEncoding(936);            //邮件附件            for (int i = 0; i < arrFilePath.Count; i++)            {                string file = arrFilePath[i].ToString(); //附件路径                Attachment data = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);                // Add time stamp information for the file.                System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;                disposition.CreationDate = System.IO.File.GetCreationTime(file);                disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);                disposition.ReadDate = System.IO.File.GetLastAccessTime(file);                // Add the file attachment to this e-mail message.                Email.Attachments.Add(data);            }            SmtpClient Client = new SmtpClient("172.30.1.13");            Client.UseDefaultCredentials = false;            Client.Credentials = new System.Net.NetworkCredential("shoa", "shnetweb.1234");            Client.DeliveryMethod = SmtpDeliveryMethod.Network;            try            {                Client.Send(Email);            }            catch (SmtpFailedRecipientsException ex)            {                for (int i = 0; i < ex.InnerExceptions.Length; i++)                {                    SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;                    if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)                    {                        //Response.Write("Delivery failed - retrying in 5 seconds.");                        System.Threading.Thread.Sleep(5000);                        Client.Send(Email);                    }                }            }            catch (Exception ex)            {                return false;            }            finally            {                for (int i = 0; i < Email.Attachments.Count; i++) //释放占用excel资源                {                    Email.Attachments[i].Dispose();                }            }            return true;        }        #endregion 


[解决办法]
http://ufo-crackerx.blog.163.com/blog/static/1130787782011111353853837/
[解决办法]

C# code
protected void But_Send_Click(object sender, EventArgs e)    {        if (!IsValid) return;        bool canSend = true;//是否允许发送        string erroMessage = "非法操作";        long resumeID = 0;        foreach (RepeaterItem ri in this.Rep_Resume.Items)        {            if (((RadioButton)ri.FindControl("RadioButton1")).Checked)            {                resumeID =Convert.ToInt64(((HiddenField)ri.FindControl("HF_RID")).Value);                HiddenField hfCanSend = (HiddenField)ri.FindControl("HF_CanSend");                DropDownList ddlOpenSet = (DropDownList)ri.FindControl("DDL_OS");                if (hfCanSend.Value == "N")                {                    canSend = false;                    erroMessage = "该简历完整度不够,不允许外发";                }                if (ddlOpenSet.SelectedValue == "3")                {                    canSend = false;                    erroMessage = "完全保密状态下的简历不允许外发";                }                break;            }        }        if (resumeID == 0)        {            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('请选择要外发的简历');", true);            return;        }        if (!canSend)        {            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + erroMessage + "');", true);            return;        }//以下是楼主想要的代码        EmailManager em = new EmailManager();//邮件发送管理类对象(自定义)        string rootURL = Utilities.GetRootURL();//网站域名         //这里是核心代码了        WebRequest req = WebRequest.Create(string.Format("{0}/JobSeeker/ResumeSendEmailTempFile/EmailTempFile.aspx?RID={1}", rootURL, resumeID));//请楼主注意了,最终要发送的邮件内容,就是这个EmailTempFile.aspx页面最终向客户端输出的完整HTML代码,至于生成的是什么内容,就看你自己喜欢了,注意,如果你页面里包括有图片或JS之类的外部文件,建议使用绝对路径,因为你的页面是发送到邮箱里的,路径不使用绝对路径,可能会出错        WebResponse res = req.GetResponse();        Stream pageStream = res.GetResponseStream();        StreamReader sr = new StreamReader(pageStream, System.Text.Encoding.GetEncoding("GB2312"));        try        {            EmailContents ec = new EmailContents();            ec.Body = sr.ReadToEnd();//这里,就是最终的要发送到邮箱里的内容了            ec.FromName = ConfigurationManager.AppSettings["SiteName"];            ec.Subject = Utilities.WipeScript(this.TB_Title.Text.Trim());            ec.To = this.TB_Email.Text;            em.SendPage(ec);        }        catch (Exception c)        {            sr.Close();            pageStream.Close();            res.Close();            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('网络超时,请重试');", true);            return;        }        finally        {            sr.Close();            pageStream.Close();            res.Close();        }        if (em.IsSent) this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('发送成功');", true);        else this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('发送失败,请检查收信人邮箱是否正确');", true);    } 

热点排行