2017-05-27 67 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()最终失败 –

相关问题