zoukankan      html  css  js  c++  java
  • 邮件发送方法(不基于模板)

    代码



    邮件发送类
    #region Send e-mail

            
    /// <summary>
            
    /// 用SMTP设置发送MailMessage邮件
            
    /// </summary>
            public static void SendMailMessage(MailMessage message)
            {
                
    if (message == null)
                    
    throw new ArgumentNullException("message");

                
    try
                {
                    message.IsBodyHtml 
    = true;
                    message.BodyEncoding 
    = Encoding.UTF8;
                    SmtpClient smtp 
    = new SmtpClient(BlogSettings.Instance.SmtpServer);
                    
    // don't send credentials if a server doesn't require it,
                    
    // linux smtp servers don't like that 
                    if (!string.IsNullOrEmpty(BlogSettings.Instance.SmtpUserName)) {
                        smtp.Credentials 
    = new System.Net.NetworkCredential(SmtpUserName,SmtpPassword);
                    }
                    smtp.Port 
    = SmtpServerPort;
                    smtp.EnableSsl 
    = EnableSsl;//bool型
                    smtp.Send(message);
                    OnEmailSent(message);
                }
                
    catch (SmtpException)
                {
                    OnEmailFailed(message);
                }
                
    finally
                {
                    
    // 关闭线程销毁.
                    message.Dispose();
                    message 
    = null;
                }
            }

            
    /// <summary>
            
    ///在另一个异步发送邮件对象.
            
    /// </summary>
            
    /// <param name="message">The message to send.</param>
            public static void SendMailMessageAsync(MailMessage message)
            {
                ThreadPool.QueueUserWorkItem(
    delegate { SendMailMessage(message); });
            }

            
    /// <summary>
            
    /// 邮件发送成功
            
    /// </summary>
            public static event EventHandler<EventArgs> EmailSent;
            
    private static void OnEmailSent(MailMessage message)
            {
                
    if (EmailSent != null)
                {
                    EmailSent(message, 
    new EventArgs());
                }
            }

            
    /// <summary>
            
    /// 邮件发送失败
            
    /// </summary>
            public static event EventHandler<EventArgs> EmailFailed;
            
    private static void OnEmailFailed(MailMessage message)
            {
                
    if (EmailFailed != null)
                {
                    EmailFailed(message, 
    new EventArgs());
                }
            }

            
    #endregion



    具体方法调用
    #region Send e-mail

    private bool SendEmail(string email, string name, string subject, string message)
        {
            
    try
            {
                
    using (MailMessage mail = new MailMessage())
                {
                    mail.From 
    = new MailAddress(BlogSettings.Instance.Email, name);//BlogSettings默认设置的邮件地址
                    mail.ReplyTo = new MailAddress(email, name);

                    mail.To.Add(BlogSettings.Instance.Email);
    ////BlogSettings默认设置的邮件地址
                    mail.Subject =" e-mail - " + subject;

                    mail.Body 
    = "<div style=\"font: 11px verdana, arial\">";
                    mail.Body 
    += Server.HtmlEncode(message).Replace("\n""<br />"+ "<br /><br />";//内容Html转换
                    mail.Body += "<hr /><br />";
                    mail.Body 
    += "<h3>Author information</h3>";
                    mail.Body 
    += "<div style=\"font-size:10px;line-height:16px\">";
                    mail.Body 
    += "<strong>Name:</strong> " + Server.HtmlEncode(name) + "<br />";
                    mail.Body 
    += "<strong>E-mail:</strong> " + Server.HtmlEncode(email) + "<br />";
                    
    if (HttpContext.Current != null)
                    {
                        mail.Body 
    += "<strong>IP address:</strong> " + HttpContext.Current.Request.UserHostAddress + "<br />";
                        mail.Body 
    += "<strong>User-agent:</strong> " + HttpContext.Current.Request.UserAgent;
                    }

                    
    if (txtAttachment.HasFile) //txtAttachment为上传附件服务器控件
                    {
                        Attachment attachment 
    = new Attachment(txtAttachment.PostedFile.InputStream, txtAttachment.FileName);
                        mail.Attachments.Add(attachment);
                    }

                    SendMailMessage(mail);
    //发送邮件
                }

                
    return true;
            }
            
    catch (Exception ex)
            {
                
    return false;
            }
        }

    #endregion
  • 相关阅读:
    领会一些比较巧妙的算法
    操作系统os常识
    C++中的继承与虚函数各种概念
    我学shell程序的记录
    matlab:linux环境中将m文件编译成动态链接库
    struct内存对齐:gcc与VC的差别
    fedora中丢失或损坏fstab,无法启动,如何补救
    判断一个字符串中的字符是否都在另一个中出现
    linux下的不错的小软件:apvlv,zathura和vifm
    C语言中将结构体写入文件
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/1626236.html
Copyright © 2011-2022 走看看