본문 바로가기
C#

SMTP로 메일 보내기

by 캡틴노랑이 2018. 11. 8.
반응형

DevExpress의 Report의 PDF 문서를  MemoryStream으로 받은 다음에 C#의 SMTP를 이용하여 메일로 전송.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//using System.Net;
//using System.Net.Mail;
 
 
using (MemoryStream stream = new MemoryStream())
{
    report.ExportToPdf(stream);
    byte[] bytes = stream.ToArray();
 
    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("mail.test.co.kr");
        string msg = GetMailTempleate();
 
        msg = msg.Replace("[MailTitle]", "발주서 자리");
        msg = msg.Replace("[name]", ds.Tables[0].Rows[0]["USRNM"].ToString());
        //msg = msg.Replace("[Position]", ds.Tables[0].Rows[0][""].ToString());
        //msg = msg.Replace("[part]", "");
        msg = msg.Replace("[address]", ds.Tables[0].Rows[0]["ADDR"].ToString());
        msg = msg.Replace("[phone]", ds.Tables[0].Rows[0]["OFFICE_TEL"].ToString());
        msg = msg.Replace("[fax]", ds.Tables[0].Rows[0]["FAX_NUM"].ToString());
 
        mail.From = new MailAddress("finalwarrior@naver.com");
        mail.To.Add(new MailAddress("finalwarrior@naver.com"));
        mail.Subject = "발주서 명칭???";
        mail.Body = msg;
                                        
        mail.Attachments.Add(new Attachment(new MemoryStream(bytes), "test.pdf" ));//, "application/pdf"
        mail.IsBodyHtml = true;
 
        SmtpServer.Port = 5878;
        SmtpServer.Credentials = new System.Net.NetworkCredential("test@test.co.kr", "*******");
        SmtpServer.EnableSsl = true;
        //SmtpServer.Timeout = 5;
 
        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}


반응형

댓글