如何在 C# 中对 SMTP 进行身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/298363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make SMTP authenticated in C#
提问by ecleel
I create new ASP.NET web application that use SMTP to send message. The problem is the smtp was not authenticated from who send the message.
我创建了使用 SMTP 发送消息的新 ASP.NET Web 应用程序。问题是 smtp 未从发送消息的人处进行身份验证。
How can I make SMTP authenticated in my program? does C# have a class that have attribute for enter username and password?
如何在我的程序中对 SMTP 进行身份验证?C# 有一个具有输入用户名和密码属性的类吗?
采纳答案by Arief
using System.Net;
using System.Net.Mail;
using(SmtpClient smtpClient = new SmtpClient())
{
var basicCredential = new NetworkCredential("username", "password");
using(MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("[email protected]");
smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
// Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("[email protected]");
try
{
smtpClient.Send(message);
}
catch(Exception ex)
{
//Error, could not send the message
Response.Write(ex.Message);
}
}
}
You may use the above code.
您可以使用上面的代码。
回答by Tor Haugen
How do you send the message?
你如何发送消息?
The classes in the System.Net.Mail
namespace (which is probably what you should use) has full support for authentication, either specified in Web.config, or using the SmtpClient.Credentials
property.
System.Net.Mail
命名空间中的类(这可能是您应该使用的)完全支持身份验证,无论是在 Web.config 中指定,还是使用SmtpClient.Credentials
属性。
回答by OJ.
Set the Credentialsproperty before sending the message.
在发送消息之前设置Credentials属性。
回答by Joe King
Ensure you set SmtpClient.Credentials
aftercalling SmtpClient.UseDefaultCredentials = false
.
确保SmtpClient.Credentials
在调用后进行设置SmtpClient.UseDefaultCredentials = false
。
The order is important as setting SmtpClient.UseDefaultCredentials = false
will reset SmtpClient.Credentials
to null.
顺序很重要,因为设置SmtpClient.UseDefaultCredentials = false
将重置SmtpClient.Credentials
为 null。
回答by Jason Dang
To send a message through TLS/SSL, you need to set Ssl of the SmtpClient class to true.
要通过 TLS/SSL 发送消息,您需要将 SmtpClient 类的 Ssl 设置为 true。
string to = "[email protected]";
string from = "[email protected]";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Send(message);
回答by YoniXw
In my case even after following all of the above. I had to upgrade my project from .net 3.5 to .net 4 to authorize against our internal exchange 2010 mail server.
在我的情况下,即使在遵循上述所有内容之后。我必须将我的项目从 .net 3.5 升级到 .net 4 才能对我们的内部 Exchange 2010 邮件服务器进行授权。