이메일 전송을 위한 NetLogic 개발
이 NetLogic은 미리 정의된 주소로 이메일을 보냅니다.
- NetLogic을 만듭니다.
- 프로젝트 보기에서NetLogic을 마우스 오른쪽 버튼으로 클릭하고 을 선택합니다.
- NetLogic 위로 마우스를 가져가서을 선택하고EmailSender를 입력합니다.
- NetLogic을 두 번 클릭합니다.외부 코드 편집기가 열립니다.
- 코드 편집기에서 다음을 수행합니다.
- 기존 코드를 다음과 같은 코드로 바꿉니다.using System; using System.Net.Mail; using System.Net; using UAManagedCore; using FTOptix.NetLogic; public class EmailSender : BaseNetLogic { [ExportMethod] public void SendEmail(string replyToAddress, string mailSubject, string mailBody) { if (string.IsNullOrEmpty(replyToAddress) || mailSubject == null || mailBody == null) { Log.Error("EmailSender", "Invalid values for one or more parameters."); return; } var fromAddress = new MailAddress("mail@domain.com", "Name"); // Email Sender var toAddress = new MailAddress("mail@domain.com", "Name"); // Email Receiver // Password for SMTP server authentication if necessary const string fromPassword = "Insert your password here."; var smtpClient = new SmtpClient { // Fill the following lines with your SMTP server info Host = "smtp.domain.com", Port = 587, EnableSsl = true, // Set to true if the server requires SSL. DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; var message = new MailMessage() { // Create the message. Subject = mailSubject, Body = mailBody }; // Specify the sender message.From = fromAddress; // Recipient emails // The MailMessage.To property is a collection of emails, so you can add different recipients using: // message.To.Add(new MailAddress(...)); message.To.Add(toAddress); // Add reply-to address message.ReplyToList.Add(replyToAddress); try { // Send email message smtpClient.Send(message); Log.Info("Message " + mailSubject + " sent successfully."); } catch (Exception ex) { // Insert here actions to be performed in case of an error when sending an email Log.Error("Error while sending message " + mailSubject + ", please try again. " + ex.Message); } } }팁: 예제에서는 런타임 시 입력한 이메일이ReplyToList속성에 추가됩니다. 이 속성에는 응답 시 받는 사람으로 자동으로 추가되는 주소 목록이 포함되어 있습니다.SmtpClient의Send메서드는 가능한 예외를 처리하기 위해 try/catch 구문 내에 캡슐화됩니다.
- 보낸 사람 이메일을 반영하도록var fromAddress변수 값을 변경합니다.
- 받는 사람 이메일을 반영하도록var toAddress변수 값을 변경합니다.팁: 이 예제에서는 이메일이 받는 사람 한 명에게 전송되지만MailMessage클래스의To속성을 사용하여 받는 사람을 더 추가할 수 있습니다. MailMessage.To Property (System.Net.Mail) | Microsoft Docs 항목을 참조하십시오.예:mailMessage.To.Add(new MailAddress(...)
- 필요한 경우const string fromPassword = "Insert your password here.";를 수정하여 이메일 계정에 암호를 제공합니다.중요: 이메일 계정을 보호하기 위해 이중 인증을 사용하는 경우 응용 프로그램에 대한 암호를 생성해야 합니다.
- 필요한 경우var smtpClient변수 값에서Credentials데이터를 구성합니다.
- 코드를 저장합니다.
의견을 작성 부탁드립니다.