You're here: Snippet Directory » Microsoft .NET (154)
Language:

SendMail - E-Mails mit Text- oder HTML-Templates mit C# und .NET verschicken

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 4/25/2006
Views: 1772

Description

Die Klasse ermöglicht den Versand von E-Mails mit .NET über Templates. In diesen Templates können Platzhalter platziert werden, die dann dynamisch ersetzt werden.

Das spart vor allem das müßige Zusammensetzen von einzelnen Mail-Texten und ermöglicht eine einfachere Pflege über Text- oder HTML-Templates.

Code

1 using System; 2 using System.IO; 3 using System.Collections; 4 using System.Net.Mail; 5 using System.Text; 6 using System.Web; 7 8 namespace EasyBaseKernel.Bll.Utilities 9 { 10 /// <summary> 11 /// SendMail-Klasse zum Parsen von Text- oder HTML-Templates und 12 /// anschließendem E-Mail-Versand 13 /// 14 /// (c) 2004 Thomas Bandt ;-) 15 /// Erweitert/umgestellt auf 2.0 am 31.05.2005 16 /// </summary> 17 public class SendMail 18 { 19 20 #region Constructor 21 22 public SendMail() 23 { 24 } 25 26 #endregion 27 28 #region Properties 29 30 private string _to; 31 32 public string To 33 { 34 get 35 { 36 return _to; 37 } 38 set 39 { 40 _to = value; 41 } 42 } 43 44 private string _from; 45 46 public string From 47 { 48 get 49 { 50 return _from; 51 } 52 set 53 { 54 _from = value; 55 } 56 } 57 58 private string _cc; 59 60 public string CC 61 { 62 get 63 { 64 return _cc; 65 } 66 set 67 { 68 _cc = value; 69 } 70 } 71 72 private string _bcc; 73 74 public string Bcc 75 { 76 get 77 { 78 return _bcc; 79 } 80 set 81 { 82 _bcc = value; 83 } 84 } 85 86 private string _subject; 87 88 public string Subject 89 { 90 get 91 { 92 return _subject; 93 } 94 set 95 { 96 _subject = value; 97 } 98 } 99 100 private string _replyTo; 101 102 public string ReplyTo 103 { 104 get 105 { 106 return _replyTo; 107 } 108 set 109 { 110 _replyTo = value; 111 } 112 } 113 114 private string _smtp; 115 116 public string Smtp 117 { 118 get 119 { 120 return _smtp; 121 } 122 set 123 { 124 _smtp = value; 125 } 126 } 127 128 private string _template; 129 130 public string Template 131 { 132 get 133 { 134 return _template; 135 } 136 set 137 { 138 _template = value; 139 } 140 } 141 142 #endregion 143 144 #region Parameter 145 146 private Hashtable Parameters = new Hashtable(); 147 148 public void Add(string parameter, string _value) 149 { 150 Parameters.Add(parameter, _value); 151 } 152 153 #endregion 154 155 #region MailBody 156 private string Body() 157 { 158 159 StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(Template), System.Text.Encoding.Default); 160 StringBuilder body = new StringBuilder(sr.ReadToEnd()); 161 sr.Close(); 162 163 if (Parameters != null) 164 { 165 foreach (DictionaryEntry pm in Parameters) 166 { 167 body.Replace("[" + pm.Key.ToString() + "]", pm.Value.ToString()); 168 } 169 } 170 171 byte[] _body = Encoding.Unicode.GetBytes(body.ToString()); 172 173 return Encoding.Unicode.GetString(_body); 174 175 } 176 #endregion 177 178 #region MailFormat 179 180 private static bool Format(string Template) 181 { 182 string Extension = Template.Substring(Template.LastIndexOf("."), Template.Length - Template.LastIndexOf(".")); 183 if (string.Compare(Extension, ".html", true, System.Globalization.CultureInfo.CurrentCulture) == 0 || string.Compare(Extension, ".htm", true, System.Globalization.CultureInfo.CurrentCulture) == 0) 184 { 185 return true; 186 } 187 else 188 { 189 return false; 190 } 191 } 192 193 #endregion 194 195 #region Send Mail 196 197 public void Send() 198 { 199 200 try 201 { 202 203 MailMessage msg = new MailMessage(); 204 205 if (CC != null) 206 msg.CC.Add(CC); 207 208 if (Bcc != null) 209 msg.Bcc.Add(Bcc); 210 211 if (ReplyTo != null && ReplyTo.Length > 0) 212 msg.Headers.Add("Reply-To", ReplyTo); 213 214 msg.From = new MailAddress(From); 215 msg.To.Add(To); 216 msg.Subject = Subject; 217 msg.IsBodyHtml = Format(Template); 218 msg.Body = Body(); 219 220 SmtpClient smtp = new SmtpClient(); 221 smtp.Host = Smtp; 222 smtp.Send(msg); 223 224 } 225 catch (Exception e) 226 { 227 throw new Exception("Es ist ein Fehler aufgetreten: " + e.Message, e.InnerException); 228 } 229 } 230 231 #endregion 232 233 } 234 }

One comment

1

prova

Wednesday, October 01, 2008 12:07:17 PM from e. bor.

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS