2011-02-12 7 views
4

私は、電子メールを送り、いくつかのコードをクリーンアップしようとしている、私のアプリを通じてつもりです。私は自分のemailerラッパークラスを作成し始めましたが、そこに標準のemailerクラスがなければならないと考えました。私はいくつかの検索をしましたが、何も見つかりませんでした。標準のemailerクラスですか?

また、このどこかのようなもののためのコードベースはありますか?

EDIT:申し訳ありませんが、私は明確にしましょう。

私は私のコードで私は電子メールを送信する必要がある任意の時間にこれを持ってしたくない:

SendEmail(string to, string from, string body) 
SendEmail(string to, string from, string body, bool isHtml) 
:私は同じような機能が含まれていEmailerの名前のクラスを作成し
System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage(); 
message.From="from e-mail"; 
message.To="to e-mail"; 
message.Subject="Message Subject"; 
message.Body="Message Body"; 
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address"; 
System.Web.Mail.SmtpMail.Send(message); 

そして私はちょうど電子メール送信するために私のコードでは、この単一の行を置くことができます。

Emailer.SendEmail("[email protected]", "[email protected]", "My e-mail", false); 

私が意味を、それはあまりにもありません私はそこに標準の、受け入れられた解決策があると思った。

+1

明確にしてください。 「標準のemailerクラス」とはどういう意味ですか?フォームフィールドを電子メールに変換する何か? – anon

+0

@anon、合意しました。私はOPが単にhttp://msdn.microsoft.com/en-us/library/system.net.mail.aspxについて尋ねるだけではないと思いますか? –

+0

@Kirk Woll実際には、私は彼がいるという印象を受ける。 – GWLlosa

答えて

7

このような何か?

using System; 
using System.Net; 
using System.Net.Mail; 
using System.Net.Mime; 
using MailMessage=System.Net.Mail.MailMessage; 

class CTEmailSender 
{ 
    string MailSmtpHost { get; set; } 
    int MailSmtpPort { get; set; } 
    string MailSmtpUsername { get; set; } 
    string MailSmtpPassword { get; set; } 
    string MailFrom { get; set; } 

    public bool SendEmail(string to, string subject, string body) 
    { 
     MailMessage mail = new MailMessage(MailFrom, to, subject, body); 
     var alternameView = AlternateView.CreateAlternateViewFromString(body, new ContentType("text/html")); 
     mail.AlternateViews.Add(alternameView); 

     var smtpClient = new SmtpClient(MailSmtpHost, MailSmtpPort); 
     smtpClient.Credentials = new NetworkCredential(MailSmtpUsername, MailSmtpPassword); 
     try 
     { 
      smtpClient.Send(mail); 
     } 
     catch (Exception e) 
     { 
      //Log error here 
      return false; 
     } 

     return true; 
    } 
} 
+0

はい、ありがとうございます。あなたがこれを得たコードベースはありますか?またはこのようなコードサービスを持つコードベースについて知っていますか? – Steven

+0

@スティーブン:私はちょうど私のプロジェクトコードからクラスを取った:70-536トレーニングの本は電子メールを送信することについての良い章があります。 – LukLed

+0

初心者の質問のように聞こえるかもしれません。ステートメント "using MailMessage = System.Net.Mail.MailMessage;"とは何ですか?平均?すでにSystem.Net.Mailをインポートしているので、あなたのコードはそれなしで動作するはずです。 –

1

たぶん、あなたはSmtpClientを探していますか?

0

this old Stack Overflow Answerからなる汎用クラスを使用します。
これを試してください。


public bool SendEmail(MailAddress toAddress, string subject, string body) 
{ 
    MailAddress fromAddress = new MailAddress("pull from db or web.config", "pull from db or web.config"); 
    string fromPassword = "pull from db or config and decrypt"; 
    string smtpHost = "pull from db or web.config"; 
    int smtpPort = 587;//gmail port 

    try 
    { 
     var smtp = new SmtpClient 
     { 
      Host = smtpHost, 
      Port = smtpPort, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = false, 
      Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
     }; 
     using (var message = new MailMessage(fromAddress, toAddress) 
     { 
      Subject = subject, 
      Body = body, 
      IsBodyHtml = true 
     }) 
     { 
      smtp.Send(message); 
     } 
     return true; 
    } 
    catch (Exception err) 
    { 
     Elmah.ErrorSignal.FromCurrentContext().Raise(err); 
     return false; 
    } 

} 
0

これは私のプロジェクトの一つからの抜粋です。これは他の実装よりも少し面白いです。

public MailMessage createMailMessage(string toAddress, string fromAddress, string subject, string template) 
{ 
// Validate arguments here... 

// If your template contains any of the following {tokens} 
// they will be replaced with the values you set here. 
var replacementDictionary = new ListDictionary 
     { 
      // Replace with your own list of values 
      { "{first}", "Pull from db or config" }, 
      { "{last}", "Pull from db or config" } 
     }; 

// Create a text view and HTML view (both will be in the same email) 
// This snippet assumes you are using ASP.NET (works w/MVC) 
// if not, replace HostingEnvironment.MapPath with your own path. 
var mailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".txt"), IsBodyHtml = false }; 
var htmlMailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".htm"), IsBodyHtml = true }; 

MailMessage htmlMessage = htmlMailDefinition.CreateMailMessage(email, replacementDictionary, new Control()); 
MailMessage textMessage = mailDefinition.CreateMailMessage(email, replacementDictionary, new Control()); 

AlternateView plainView = AlternateView.CreateAlternateViewFromString(textMessage.Body, null, "text/plain"); 
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlMessage.Body, null, "text/html"); 

var message = new MailMessage { From = new MailAddress(from) }; 

message.To.Add(new MailAddress(toAddress)); 
message.Subject = subject; 

message.AlternateViews.Add(plainView); 
message.AlternateViews.Add(htmlView); 

return message; 
} 
テキストとHTMLビューの両方が含まれているランタイム
  • メールで実際の値に置き換えることができます

    • トークン:この機能を使用して

      は、あなたが電子メールを作成することができます

    Web.configがNetMail用に設定されていると仮定すると、この方法はヘルパーメソッドから呼び出すことができます。

    public bool SendEmail(MailMessage email) 
    { 
        var client = new SmtpClient(); 
    
        try 
        { 
         client.Send(message); 
        } 
        catch (Exception e) 
        { 
         return false; 
        } 
    
        return true; 
    } 
    
    SendMail(createMailMessage("[email protected]", "[email protected]", "Subject", "~/Path/Template")); 
    
  • 関連する問題