【发布时间】:2013-03-05 23:18:24
【问题描述】:
我正在使用using System.Net.Mail;
以下代码发送邮件
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_here
当我使用client.Host = "smtp.gmail.com";
我收到以下错误
连接尝试失败,因为连接方没有 一段时间后正确响应,或建立连接 失败,因为连接的主机没有响应
我无法通过 localhost 发送邮件。 请帮帮我,我是 C# 新手,请在我出错的代码中纠正我..?
【问题讨论】:
-
您需要一个愿意为您发送邮件的 SMTP 服务器。 (以及登录名、正确的端口和 SSL 设置)
-
如何从我的机器发送邮件,我没有部署代码。我正在本地主机上工作。当我使用主机“smtp.gmail.com”时;它失败了..我不知道如何更正端口和 SSL 设置。
标签: c# email smtpclient