관리 메뉴

도드넷

유니티 안드로이드 C# 메일 보내기 - 버그 리포트 본문

창고/게임 개발 [Hidden]

유니티 안드로이드 C# 메일 보내기 - 버그 리포트

도드! 2022. 11. 29. 13:57
반응형

유니티 안드로이드 C# 메일 보내기 - 버그 리포트 만들기.

 

 

1. 목표 : 사용자가 앱에서 보고자용 앱을 통해서 관리자에게 메일을 보낼 수 있게 만든다.

 

2. 보고자 전용 새로운 구글 메일 계정 만들기.

 

3. 해당 구글 계정설정 보안에서 App password 만들고 복사해놓기.

https://myaccount.google.com/security?hl=en

4. 다음 코드 사용하기.

 

필요한 using 선언

using System;

using System.IO;

using System.Collections;

using System.Collections.Generic;

using System.Net;

using System.Net.Mail;

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

using System.ComponentModel;

 

메일 보내기를 수행할 메인코드. 생각보다 간단함. 

mono에 넣고 싶으면 Program 이하만 복사해서 새 mono class에 넣고 스레드 관련 커맨드 지우면됨.

class Program
    {
        private const string SmtpServer = "smtp.gmail.com";
        private const string MailserverLogin = "ddddd@gmail.com";
        private const string MailServerPassword = "wohpkockczwcjznp";
        private const string MailUserName = "Developer tips support";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            // SmtpClient
            var client = new SmtpClient(SmtpServer)
            {
                Port = 587,
                Credentials = new System.Net.NetworkCredential(MailserverLogin, MailServerPassword),
                EnableSsl = true
            };
            
            // Specify the email sender.
            var from = new MailAddress(MailserverLogin, MailUserName, System.Text.Encoding.UTF8);
            
            // Set destinations for the email message.
            var to = new MailAddress("xxxxx@daimto.com");
            
            // Specify the message content.
            var message = new MailMessage(@from, to)
            {
                Body = "This is a test email message sent by an application. ",
                Subject = "Customer support Daimto." 
            };
            
            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            
            // The userState can be any object that allows your callback
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            var userState = "test message1";
            client.SendAsync(message, userState);

            
            while (!mailSent)
            { 
                Console.Write(".");
                Thread.Sleep(50);
            } 
            
            //clean up.
            message.Dispose();
            client.Dispose();

        }

        
        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            var token = (string) e.UserState;

            if (e.Cancelled)
            {
                Console.WriteLine("[{0}] Send canceled.", token);
            }

            if (e.Error != null)
            {
                Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            }
            else
            {
                Console.WriteLine("Message sent.");
            }

            mailSent = true;
        }
    }

 

 

첨부 파일은 다음 코드를 사용하면 된다.

        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment("경로"); 
        mail.Attachments.Add(attachment);

 

메모.

1. 이것은 사용자의 구글 메일 계정을 사용하지 않고 [보고용 계정]을 사용한다.

2. 위의 코드에서 Credential 을 설정할때 비밀번호는 메일 계정 비밀번호가 아니라 3번에서 설정한 App Password 다.

3. IOS는 테스트 해보지 않아서 모르겠는데 코드가 전부 C#이라 안될 이유는 없는것 같다.

4. 메인코드가 유니티 C#이 아니므로 코루틴화 해서 적당한 메일 보내기 루틴을 만들어주자. (스레드 wait -> yield return wait...)

 

 

참고 문서

https://stackoverflow.com/questions/69259641/send-email-via-gmail-through-c-sharp-application

http://minpaprograming.blogspot.com/2018/06/unity-send-email.html

반응형
Comments