[C# , SMTP]구글 SMTP 로 메일 보내기
Gmail 계정을 이용해 별도의 메일 서버 없이 E 메일을 발송할수가 있다.
아래는 이해를 돕기위해 변수 , 메서드를 나열하였다.(실 개발에서는 좀더 세련된 코드로...)
Created with colorer-take5 library. Type 'csharp' private void SendMail() { // //필요한 정보 , id , pw 는 Gmail 계정을 입력한다. // String id = "****@gmail.com"; String pw = "****"; String from = "****@gmail.com"; String mailto = "totototo@nate.com"; String title = "TITLE2"; String htmlStr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"; //다중 첨부파일 String[] attachFiles = new string[]{"c:/test.jpg" , "c:/test.txt"}; //mail msg MailMessage msg = new MailMessage(from , mailto , title , htmlStr); msg.IsBodyHtml = true; msg.BodyEncoding = System.Text.Encoding.UTF8; msg.SubjectEncoding = System.Text.Encoding.UTF8; //tokenList List<string> userToken_list = new List<string>(); userToken_list.Add(mailto); //attach file if (attachFiles != null) { foreach (string file in attachFiles) { if (System.IO.File.Exists(file)) { msg.Attachments.Add(new Attachment(file)); userToken_list.Add(file); } } } //Array 형태로 바꿈 String[] userToken = userToken_list.ToArray(); //smtp 구글 설정 SmtpClient sc = new SmtpClient("smtp.gmail.com", 587); sc.Credentials = new NetworkCredential(id, pw); sc.EnableSsl = true; sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted); sc.SendAsync(msg, userToken); } |
결과 : 첨부파일 2개와 HTML 텍스트가 정상적으로 들어왔다.
++++ 추가 ++++
첨부파일을 바로 메일 본문에 삽입하기 (ContentId 지정!)
첨부파일은 바로 new Attachment(...) 를 하지말고 변수 인스턴스를 잡는다.
Attachment at = new Attachment(file , MediaTypeNames.Image.Jpeg);
at.ContentId = "ContentID0";
msg.Attachments.Add(at);
하고 HTML 에는 , <img src="cid:ContentID0"> 처럼 'cid:' 를 지정하면 됨
'C#' 카테고리의 다른 글
[C# , AS3 , ETC] 한글 유니 코딩 조합 원리 (115) | 2011.11.21 |
---|---|
[C# 서버에서 AS3 클라이언트 비정상 종료 체크하기] (164) | 2011.10.16 |
[C# , SMTP]구글 SMTP 로 메일 보내기 (97) | 2011.08.22 |
[TwitPic 모듈] 트윗픽을 손쉽게 활용하자 (998) | 2011.06.09 |
[WPF] WebBrowser 에서 Documents 보기 (html) (93) | 2011.05.31 |
[Directory Copy] 디렉토리 통채로 카피하기 (61) | 2011.05.23 |
C#
2011.08.22 17:30