이 단계별 가이드에서는 .NET용 Aspose.Email을 사용하여 C#에서 이메일을 보내는 방법을 공유합니다. 우리는 이 C# 콘솔 애플리케이션에서 Gmail SMTP 서버를 사용하여 이메일을 보내고 있지만 모든 유형의 .NET 애플리케이션에서 모든 SMTP 서버를 사용하여 이메일을 보낼 수 있습니다.
C#에서 이메일을 보내는 단계
- NuGet 패키지 관리자에서 Aspose.Email for .NET 설정
- Aspose.Email, Aspose.Email.Clients 및 Aspose.Email.Clients.Smtp 네임스페이스를 포함합니다.
- Aspose.Email.License 클래스를 사용하여 라이선스 적용
- MailMessage 클래스를 사용하여 새 이메일 메시지 만들기
- SmtpClient 클래스의 인스턴스 생성
- 이메일을 보낼 Gmail SMTP 클라이언트 정보 설정
- SmtpClient 클래스의 Send 메서드를 사용하여 email 보내기
C#을 사용하여 이메일을 보내는 코드
using System; | |
//Add Aspose.Email for .NET package reference | |
//Use following namespaces to convert OTG to PDF format | |
using Aspose.Email; | |
using Aspose.Email.Clients; | |
using Aspose.Email.Clients.Smtp; | |
namespace SendEmailUsingSMTPServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before sending email through Gmail SMTP | |
//using Aspose.Email for .NET | |
Aspose.Email.License AsposeEmailLicense = new Aspose.Email.License(); | |
AsposeEmailLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//create an instance of MailMessage | |
MailMessage EmailMessage = new MailMessage(); | |
//Set email message properties which you want to specify | |
EmailMessage.Subject = "How to Send Mail Using SMTP Server in C#"; | |
EmailMessage.To = "ReceiverEmail@EmailServer.com"; | |
EmailMessage.Body = "This is a test of sending email using SMTP in C#."; | |
//Initiate an instance of SmptpClient class | |
SmtpClient SMTPEmailClient = new SmtpClient(); | |
//Set SMTP client properties so the email message can get through the server | |
SMTPEmailClient.Host = "smtp.gmail.com"; | |
SMTPEmailClient.Username = "YourEmail@gmail.com"; | |
SMTPEmailClient.Password = "Your Gamil Password"; | |
SMTPEmailClient.Port = 587; | |
SMTPEmailClient.SecurityOptions = SecurityOptions.SSLExplicit; | |
//Finally send the email message using Gmail's SMTP client | |
SMTPEmailClient.Send(EmailMessage); | |
} | |
} | |
} |
C#에서 Gmail SMTP를 사용하여 이메일을 보낼 때 문제
“SMTP 서버에 보안 연결이 필요하거나 클라이언트가 인증되지 않았습니다.“와 같은 예외가 발생할 수 있습니다. Gmail은 귀하의 애플리케이션이 덜 안전한 것으로 간주하여 SMTP 서버를 사용하여 이메일을 보내는 것을 허용하지 않을 수 있습니다. Gmail SMTP 문제를 해결하려면 allow access to less secure apps to send email.