尝试创建服务以发送邮件但收到错误

时间:2017-05-27 12:23:09

标签: c# jquery html ajax web-services

我创建了一个发送邮件但收到错误的服务:

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("user@example.net", "user@example.org"))
            {
                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("user@example.net", "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似乎是错误的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你的错误告诉你什么? (顺便说一句,如果你想要消息接受字符串作为html你必须设置mm.IsBodyHtml = true;,而不是你现在设置的错误