邮件发送方法(不基于模板)

代码



邮件发送类
#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

 

你可能感兴趣的:(邮件发送)