2017-05-27 40 views
0

我創建了一個服務來發送郵件,但收到錯誤:試圖建立一個服務來發送郵件,但收到錯誤

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Mail; 
using System.Web; 
using System.Web.Script.Serialization; 
using System.Web.Services; 
using System.IO; 
using System.Net; 
using System.Net.Mail; 

    /// <summary> 
    /// Summary description for Thankyou 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // 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 Thankyou : System.Web.Services.WebService 
    { 

     public Thankyou() 
     { 

      //Uncomment the following line if using designed components 
      //InitializeComponent(); 
      using (MailMessage mm = new MailMessage("[email protected]", "[email protected]")) 
      { 
       mm.Subject = "Event Inquiry"; 
       mm.Body = @"<html> 
    <head><title>[#text:@@hotelname#] Event Inquiry Form</title></head> 
    <body> 
    <h1 style = ""font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:normal; font-size:14px; ""> You have received an email via the <b>[Culver Hotel </b> Event Inquiry form.</h1> 
    <h2 style = ""font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:normal; font-size:12px;"" > 
    <b> From: </b>[#text:contact_name#] <a href=""mailto:[#text:contact_email#]"" >[#text:contact_email#]</a><br> 
    <b> Phone: </b> [#text:contact_phone#]<br> 
    <b> Requested Date: </b> [#text:rfp_requestmonth#] [#text:rfp_requestdate#],[#text:rfp_requestyear#]<br> 
    <b> Requested Time: </b> [#text:requested_time#]<br> 
    <b> Number of Guests: </b> [#text:number_guests#]<br> 
    <b> Comments: </b><br> [#text:contact_comments#] 
    </h2> 
    </body> 
    </html>"; 
       //if (fuAttachment.HasFile) 
       //{ 
       // string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName); 
       // mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName)); 
       //} 
       mm.IsBodyHtml = false; 
       SmtpClient smtp = new SmtpClient(); 
       smtp.Host = "smtp.gmail.com"; 
       smtp.EnableSsl = true; 
       NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "passw0rd"); 
       smtp.UseDefaultCredentials = true; 
       smtp.Credentials = NetworkCred; 
       smtp.Port = 587; 
       smtp.Send(mm); 
       //ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true); 
      } 
     } 

     [WebMethod] 
     public string ThanksMail() 
     { 
      //return "Hello World"; 
      Dictionary<string, string> name = new Dictionary<string, string>(); 
      name.Add("1", "Sourav Kayal"); 
      name.Add("2", "Ram mishra"); 
      string myJsonString = (new JavaScriptSerializer()).Serialize(name); 
      return myJsonString; 
     } 

    } 

我得到一個錯誤,在這一點上:smtp.Send(mm);,但HTML似乎是正確的。

任何幫助將不勝感激。

+0

發佈錯誤本身將會非常有助於試圖幫助你的ppl – stevenvanc

回答

0

你的錯誤告訴你什麼? (順便說一句,如果你想要郵件接受字符串作爲html,你必須設置mm.IsBodyHtml = true ;,因爲你現在設置它不是假的

+0

您好以下是錯誤「在System.dll中發生類型'System.Net.Mail.SmtpException'的異常,但未在用戶代碼中處理 其他信息:發送郵件失敗。」 – miki

+0

https://myaccount.google。 com/lesssecureapps試圖啓用它,因爲你試圖從你的應用程序登錄它會阻止你,並且SendMail()最終失敗 –

相關問題