我们可以使用asp.net和c#从localhost发送邮件吗?
我使用using System.Net.Mail;
我们可以使用asp.net和c#从localhost发送邮件吗?
和下面的代码来发送邮件
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
// Set the sender's address
message.From = new MailAddress("fromAddress");
// Allow multiple "To" addresses to be separated by a semi-colon
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))
{
message.To.Add(new MailAddress(addr));
}
}
// Allow multiple "Cc" addresses to be separated by a semi-colon
if (ccAddress.Trim().Length > 0)
{
foreach (string addr in ccAddress.Split(';'))
{
message.CC.Add(new MailAddress(addr));
}
}
// Set the subject and message body text
message.Subject = subject;
message.Body = messageBody;
// Set the SMTP server to be used to send the message
client.Host = "YourMailServer";
// Send the e-mail message
client.Send(message);
的主机我提供client.Host = "localhost";
此其下降与错误
无连接可以作出因为目标机器积极 拒绝它 some_ip_address_he再
,当我使用client.Host = "smtp.gmail.com";
我获得以下错误
连接尝试失败,因为连接的方没有 一段时间后正确响应或已建立的连接 因连接的主机无法响应而失败
我无法通过本地主机发送邮件。 请帮助我,我是新来的C#请纠正我在代码中我错了..?
下面是一些代码,用于通过Gmail发送邮件工作(代码从某处这里计算器它类似于此代码:Gmail: How to send an email programmatically):
using (var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("[email protected]", "yourpassword"),
EnableSsl = true
})
{
client.Send("[email protected]", "[email protected]", "subject", message);
}
将此代码放在网页一个<configuration> </configuration>
内。配置文件
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="[email protected]" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
然后后端代码
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress(TextBoxEadd.Text));
message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "New User Registration ! ";
message.Body = "HELLO";
sr.Close();
SmtpClient client = new SmtpClient();
client.Send(message);
我希望这段代码能帮助你! :)
这为我工作!但你能告诉我为什么你需要定义message.From =新的MailAddress(“[email protected]”),当你在web配置中指定你的凭证,即userName =“[email protected]” – Haris 2015-01-09 21:17:55
要发送邮件从client.Host = "localhost"
您需要设置本地SMTP服务器。
要通过Google(或通过任何其他SMTP服务器,包括您自己的本地SMTP)发送邮件,您必须设置用户名,密码,ssl设置 - 所有SMTP服务器都需要您选择,并且您需要阅读他们的帮助。
例如Google says您需要SSL,端口465或587,服务器smtp.gmail.com
以及您的用户名和密码。
您可以在.config文件中分配所有这些值。
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="[email protected]" password="password" />
</smtp>
</mailSettings>
</system.net>
,或设置为SmtpClient在代码中每次使用前:
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSSL = true;
client.Credentials = new NetworkCredential("[email protected]", "password");
使用本Line..Dont使用端口和主机名
LocalClient.DeliveryMethod = SmtpDeliveryMethod。PickupDirectoryFromIis;
您需要一个愿意为您发送邮件的SMTP服务器。 (和登录,以及正确的端口和SSL设置) – SLaks 2013-03-17 16:10:39
如何从我的机器发送邮件,我不部署代码。我在本地主机上工作。当我使用主机“smtp.gmail.com”;它的faling ..我不知道如何更正端口和SSL设置。 – 2013-03-17 16:12:34