Jak mogę dokonać uwierzytelnienia SMTP w C#
Tworzę nowe ASP.NET aplikacja internetowa wykorzystująca SMTP do wysyłania wiadomości. Problem polega na tym, że smtp nie został uwierzytelniony od tego, kto wysyła wiadomość.
Jak mogę dokonać uwierzytelnienia SMTP w moim programie? czy C# ma klasę, która ma atrybut enter username i password?
6 answers
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);
}
}
}
Możesz użyć powyższego kodu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-26 07:06:16
Upewnij się, że ustawiłeś SmtpClient.Credentials
Po wywołaniu SmtpClient.UseDefaultCredentials = false
.
Kolejność jest ważna, ponieważ ustawienie SmtpClient.UseDefaultCredentials = false
spowoduje zresetowanie SmtpClient.Credentials
Na null.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-12 07:11:15
Ustaw właściwość poświadczenia przed wysłaniem wiadomości.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-11-18 10:32:23
Aby wysłać wiadomość przez TLS / SSL, musisz ustawić Ssl klasy SmtpClient na 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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-01 09:24:50
Jak wysłać wiadomość?
Klasy w przestrzeni nazw System.Net.Mail
(która prawdopodobnie jest tym, czego powinieneś użyć) mają pełne wsparcie dla uwierzytelniania, określone w Web.config, lub przy użyciu właściwości SmtpClient.Credentials
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-25 03:14:21
W moim przypadku nawet po wykonaniu wszystkich powyższych. Musiałem uaktualnić mój projekt z. Net 3.5 do. Net 4, aby autoryzować się na naszym wewnętrznym serwerze pocztowym exchange 2010.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-18 08:43:13