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
  • 相关阅读:
    【Javascript】JS单例模式的简单实现
    【Javascript】Javascript中如何判断变量是数组类型
    买卖股票的最佳时机 II
    只出现一次的数字
    删除排序数组中的重复项
    两数之和
    Android系统中Fastboot和Recovery所扮演的角色。
    虚函数、纯虚函数、抽象类、接口 (Java_C++_C#)
    关于cmd中执行命令路径包含空格的解决办法
    Windows API 编程学习记录<三>
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/1626236.html
Copyright © 2011-2022 走看看