2016-08-16 15 views
-1

私はc#とJQueryを使用して、いくつかの動的コンテンツを含む静的なHTMLをユーザーの電子メールIDに送信します。 以下は、SendEmailメソッドを呼び出すJavaScriotファイルです。C#で電子メールコンテンツとして静的なHTMLを送信するには?

$( "EmailInvoice")。invoiceEmail.asmxこれは、プロジェクトに追加されたHTMLファイルです

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Web; 

using System.Net.Mail; 
using System.Web.Services; 
using System.Web.Hosting; 

namespace meltwish 
{ 
    /// <summary> 
/// Summary description for invoiceEmail 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class invoiceEmail : System.Web.Services.WebService 
{ 


    public static string PopulateBody(string userName, string title, string url, string description) 
    { 
     string body = string.Empty; 
     using (StreamReader reader = new StreamReader(HostingEnvironment.MapPath("~/EmailTemplate.html"))) 
     { 
      body = reader.ReadToEnd(); 
     } 
     body = body.Replace("{UserName}", userName); 
     body = body.Replace("{Title}", title); 
     body = body.Replace("{Url}", url); 
     body = body.Replace("{Description}", description); 
     return body; 
    } 

    public static void SendHtmlFormattedEmail(string recepientEmail, string subject, string body) 
    { 
     MailMessage mailMessage = new MailMessage(); 
     mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["username"]); 
     mailMessage.Subject = subject; 
     mailMessage.Body = body; 
     mailMessage.IsBodyHtml = true; 
     mailMessage.To.Add(new MailAddress(recepientEmail)); 
     SmtpClient smtp = new SmtpClient(); 
     smtp.Host = ConfigurationManager.AppSettings["Host"]; 
     smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]); 
     System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 
     NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"]; 
     NetworkCred.Password = ConfigurationManager.AppSettings["Password"]; 
     smtp.UseDefaultCredentials = true; 
     smtp.Credentials = NetworkCred; 
     smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]); 
     smtp.Send(mailMessage); 
    } 

    //object sender, EventArgs e 
    [WebMethod] 
    public static string SendEmail() 
    { 

     //string body = this.PopulateBody("John", 
     string body = PopulateBody("John", 
      "Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender", 
      "http://www.aspsnippets.com/Articles/Fetch-multiple-values-as-Key-Value-pair-" + 
      "in-ASP.Net-AJAX-AutoCompleteExtender.aspx", 
      "Here Mudassar Ahmed Khan has explained how to fetch multiple column values i.e." + 
      " ID and Text values in the ASP.Net AJAX Control Toolkit AutocompleteExtender" 
      + "and also how to fetch the select text and value server side on postback"); 
     SendHtmlFormattedEmail("[email protected]", "New article published!", body); 
     //this.SendHtmlFormattedEmail("[email protected]", "New article published!", body); 
     return "sajjad"; 
    } 


} 
} 

ファイルで以下(関数(){

$.ajax({ 
     type: 'POST', 
     url: siteUrl + '/invoiceEmail.asmx/SendEmail', 
     data: JSON.stringify({ }), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: true, 
     success: function (data, status) {    

     }, 
     failure: function (data) { 

     }, 
     error: function() { 
      alert("error"); 
     } 
    }); 

をクリックしてください。名前は、この私EmailTemplate.html

<meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" /> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 


    <!-- Server CDN Files -- Start --> 
    <!--<link class="temp" href="http://klcdn.meltwish.com/styles/store/1.0.2/store.css" rel="stylesheet" />--> 
    <link href="http://localhost:60339/styles/store/1.0.2/store.css" rel="stylesheet" /> 
</head> 
<body> 

    <img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /><br /><br /> 
<div style = "border-top:3px solid #22BCE5">&nbsp;</div> 
<span style = "font-family:Arial;font-size:10pt"> 
Hello <b>{UserName}</b>,<br /><br /> 
A new article has been published on ASPSnippets.<br /><br /> 
<a style = "color:#22BCE5" href = "{Url}">{Title}</a><br /> 
{Description} 
<br /><br /> 
Thanks<br /> 
ASPSnippets 
</span> 
</body> 
</html> 

ですWeb.Configファイルに追加されました。

<appSettings> 
     <add key="Host" value="smtp.gmail.com"/> 
     <add key="EnableSsl" value="true"/> 
     <add key="UserName" value="[email protected]"/> 
    <add key="Password" value="xxxxx"/> 
     <add key="Port" value="587"/> 
</appSettings> 

実際にjavascript ajaxメソッドを呼び出そうとすると、エラー内容が表示されます。

....私を助けて

+0

を使用して、エラーは何ですか?それは5XXです! –

答えて

0

[System.Web.Services.WebMethod] 
public static string SendEmail() 
{ 
    using (MailMessage mm = new MailMessage("From", "To")) 
    { 
    mm.Subject = "Subject "; 
    mm.Body = "<html><head></head><body> Content</body></html>"; 
    mm.IsBodyHtml=true;                
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.EnableSsl = false; 
    NetworkCredential NetworkCred = new NetworkCredential("From", "password"); 
        smtp.UseDefaultCredentials = false; 
        smtp.Credentials = NetworkCred; 
        smtp.Port = 587; 
        smtp.Timeout = 2000000; 
        smtp.Send(mm); 
        return "Success"; 
    } 
} 

私のためにC#コードの作業の後とAjaxコードに

$.ajax({ 
    type: "POST", 
    url: siteUrl + '/invoiceEmail.aspx/SendEmail', 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    datatype: "jsondata", 
    async: "true", 
    success: function (t) { alert(t); }, 
    error: function (t) { alert(t); } }) 
+0

実際にAjaxからSendEmail()を呼び出しているときに、エラーの警告が表示されています。 –

+0

データを変更:JSON.stringify({})、データ: "{}"、 – Pravin

+0

パラメータがない場合は不要JSON.stringify – Pravin

関連する問題