gpt4 book ai didi

c# - 我们可以使用 asp.net 和 c# 从本地主机发送邮件吗?

转载 作者:行者123 更新时间:2023-11-30 13:58:46 25 4
gpt4 key购买 nike

我正在使用 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";

为此它因错误而落下

No connection could be made because the target machine actively refused it some_ip_address_here

当我使用 client.Host = "smtp.gmail.com";

我得到以下错误

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我无法通过本地主机发送邮件。请帮助我,我是 c# 的新手,请在代码中纠正我哪里出错了..?

最佳答案

这里是一些用于通过 gmail 发送邮件的代码(来自 stackoverflow 上某处的代码。它类似于此处的代码:Gmail: How to send an email programmatically):

using (var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("yourmail@gmail.com", "yourpassword"),
EnableSsl = true
})
{
client.Send("frommail@gmail.com", "tomail@gmail.com", "subject", message);
}

关于c# - 我们可以使用 asp.net 和 c# 从本地主机发送邮件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com