'파일첨부'에 해당되는 글 1건

  1. 2012.10.18 [C#] GMAIL(구글메일) 이용하여 파일첨부 이메일 보내기
Dev/C#2012. 10. 18. 12:29

아래 코드 참조.
지메일 셋팅 하고, 파일 첨부해서 '비동기' 방식으로 보내기.
(주석처리된 send 는 동기 방식)
한글을 사용하기 위해 제목(subject), 내용(body) 의 인코딩을 UTF8로 셋팅해준다.

  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Net.Mail;  
  4.   
  5. namespace EmailTestApplication  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void button1_Click(object sender, EventArgs e)  
  15.         {  
  16.             try  
  17.             {  
  18.                 MailMessage mail = new MailMessage();  
  19.                 SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");  
  20.   
  21.                 //set the address  
  22.                 mail.From = new MailAddress("your g-mail address");  
  23.                 mail.To.Add("target e-mail address");  
  24.   
  25.                 //set the contents  
  26.                 mail.Subject = "Subject Test Mail";  
  27.                 mail.Body = "This is for testing SMTP mail from GMAIL";  
  28.                 mail.BodyEncoding = System.Text.Encoding.UTF8;  
  29.                 mail.SubjectEncoding = System.Text.Encoding.UTF8;  
  30.   
  31.                 //set attachment  
  32.                 System.Net.Mail.Attachment attachment;  
  33.                 attachment = new System.Net.Mail.Attachment("D:\\temp\\file.txt");  
  34.                   
  35.                 mail.Attachments.Add(attachment);  
  36.   
  37.                 //setting smtpServer  
  38.                 SmtpServer.Port = 587;  
  39.                 SmtpServer.Credentials = new System.Net.NetworkCredential("your_ID""your_password");  
  40.                 SmtpServer.EnableSsl = true;  
  41.   
  42.                 //SmtpServer.Send(mail);  
  43.                 // MessageBox.Show("mail Send");  
  44.   
  45.                 //async send mail  
  46.                 object userState = mail;  
  47.                 SmtpServer.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);  
  48.                 SmtpServer.SendAsync(mail, userState);  
  49.                   
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 MessageBox.Show(ex.ToString());  
  54.             }  
  55.   
  56.         }  
  57.   
  58.           
  59.         void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)   
  60.         {  
  61.             //Get the Original MailMessage object  
  62.             MailMessage mail = (MailMessage)e.UserState;  
  63.   
  64.             //write out the subject  
  65.             string subject = mail.Subject;  
  66.   
  67.             if (e.Error == null)  
  68.             {  
  69.                 // Send succeeded   
  70.                 string message = string.Format("Send Mail with subject [{0}]", subject);  
  71.                 MessageBox.Show(message);  
  72.             }  
  73.             else if (e.Cancelled == true)  
  74.             {  
  75.                 string message = string.Format("Send canceld for mail with subject [{0}]", subject);  
  76.                 MessageBox.Show(message);  
  77.             }  
  78.             else  
  79.             {  
  80.                 // log exception   
  81.                 string message = string.Format("Send Mail Fail - Error: [{0}]", e.Error.ToString());  
  82.                 MessageBox.Show(message);  
  83.             }  
  84.   
  85.         }  
  86.   
  87.     }  
  88. }  

Posted by 놀란