[英]Cant send email on my local host why?
為什么我無法在本地主機上發送電子郵件我收到此錯誤:未指定SMTP主機
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("fromEmail@email.com","toEmail@email.com", "Test", "Test Body");
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Send(mail);
}
}
}
假設您要使用“localhost”郵寄
試試:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("localhost", 25);
如果不:
如果郵件服務器不在您的localhost上,請指定smtp客戶端ipAddress或hostname以及smtp服務端口 。
編輯:這里有一些使用gmail發送郵件的示例代碼:
SmtpClient clientesmtp = GetSmtpClient();
MailMessage msg = new MailMessage("from@gmail.com", "to@gmail.com", "Subject","body");
msg.IsBodyHtml = true;
clientesmtp.Send(msg);
private static SmtpClient GetSmtpClient()
{
SmtpClient clientesmtp = new SmtpClient("smtp.gmail.com", 587);
clientesmtp.Credentials = new System.Net.NetworkCredential("user", "password");
clientesmtp.EnableSsl = true;
return clientesmtp;
}
默認情況下,SMTPClient 將使用應用程序或machine.config文件中<mailSettings>
定義的主機和端口。
此構造函數使用應用程序或計算機配置文件中的設置初始化新SmtpClient的Host,Credentials和Port屬性。
在默認的那些 localhost:25
。 但是,您可能已編輯machine.config以刪除該主機。
您可以編輯application.config或machine.config以包含適當的mailSettings屬性,也可以在代碼中指定適當的值。
當然,您需要在localhost上實際運行SMTP服務器,但是從您的問題我假設您這樣做了嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.