我正嘗試使用Gmail SMTP服務器發送電子郵件,但郵件始終無法發送。瀏覽器窗口關閉,Express for Web閃爍顯示此錯誤。任何幫助將不勝感激。 代碼已被列入下面Gmail Smtp消息無法發送。
代碼隱藏
using System.Net.Mail;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demos_email_demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Test Message";
myMessage.Body = "Hello World";
myMessage.From = new MailAddress("[email protected]", "Will");
myMessage.To.Add(new MailAddress("[email protected]", "Will"));
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.Send(myMessage);
}
}
的Web.Config
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="Will <[email protected]>">
<network host ="smtp.gmail.com" enableSsl="true" port="465" />
</smtp>
</mailSettings>
</system.net>
</configuration>
嘗試在SMTP設置這樣的新SmtpClient(「smtp.gmail.com」,587)expicitlely設置端口.....你甚至可能需要使用using語句,並設置SSL和諸如此類(var smtp = new SmtpClient())var credential = new NetworkCredential {UserName =「[email protected]」,//替換爲有效值 Password =「qwerty123456」//替換爲有效值 }; smtp.Credentials =憑證; smtp.Host =「smtp.gmail.com」; smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(message);} –
您還需要設置您的Gmail帳戶以允許使用smtp。 https://www.google.com/search?q=set+gmail+account+to+allow+smtp&ie=&oe= –