C #에서 SMTP를 인증하는 방법
SMTP를 사용하여 메시지를 보내는 새 ASP.NET 웹 응용 프로그램을 만듭니다. 문제는 메시지를 보낸 사람이 SMTP를 인증하지 않았다는 것입니다.
내 프로그램에서 SMTP를 인증하려면 어떻게해야합니까? C #에는 사용자 이름과 암호를 입력하는 속성이있는 클래스가 있습니까?
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("from@yourdomain.com");
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("to@anydomain.com");
try
{
smtpClient.Send(message);
}
catch(Exception ex)
{
//Error, could not send the message
Response.Write(ex.Message);
}
}
}
위의 코드를 사용할 수 있습니다.
SmtpClient.Credentials 을 호출 한 후 설정하십시오 SmtpClient.UseDefaultCredentials = false.
설정 SmtpClient.UseDefaultCredentials = false이 SmtpClient.Credentialsnull로 재설정 되므로 순서가 중요합니다 .
메시지를 보내기 전에 Credentials 속성을 설정하십시오 .
TLS / SSL을 통해 메시지를 보내려면 SmtpClient 클래스의 Ssl을 true로 설정해야합니다.
string to = "jane@contoso.com";
string from = "ben@contoso.com";
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);
메시지를 어떻게 보내나요?
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.
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.
참고URL : https://stackoverflow.com/questions/298363/how-can-i-make-smtp-authenticated-in-c-sharp
'IT TIP' 카테고리의 다른 글
| Android facebook applicationId는 null 일 수 없습니다. (0) | 2020.10.30 |
|---|---|
| Swift를 사용하여 iOS에서 오른쪽에서 왼쪽으로 뷰 컨트롤러를 표시하는 방법 (0) | 2020.10.30 |
| Firebase 클래스에 직렬화 할 속성이 없습니다. (0) | 2020.10.30 |
| 속성 텍스트 중심 정렬 (0) | 2020.10.30 |
| "noreturn"함수가 반환되는 이유는 무엇입니까? (0) | 2020.10.30 |