【问题标题】:Trying to create a service to send mail but getting error尝试创建服务以发送邮件但出现错误
【发布时间】:2017-10-28 05:56:25
【问题描述】:

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

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 似乎是正确的。

任何帮助将不胜感激。

【问题讨论】:

  • 发布错误本身对试图帮助您的人非常有帮助。

标签: c# jquery html ajax web-services


【解决方案1】:

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

【讨论】:

  • 您好,以下是错误“System.dll 中发生类型为 'System.Net.Mail.SmtpException' 的异常,但未在用户代码中处理附加信息:发送邮件失败。”
  • myaccount.google.com/lesssecureapps 尝试启用此功能,因为您正在尝试从应用程序登录,但它会阻止您并且 SendMail() 最终失败
猜你喜欢
  • 2014-03-22
  • 2012-08-23
  • 2011-04-05
  • 1970-01-01
  • 2020-06-26
  • 2020-08-04
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多