2014-09-29 2 views
1

두 번째 열 [DRAFT PATH]의 파일 이름을 추가하고 메일을 보낼 때 첨부 파일로 포함하고 싶습니다. 어떻게이 세부 정보를 첨부 파일로 추가 할 수 있으며 본문에 텍스트 정보를 포함해야합니다.메일을 보낼 때 첨부 파일로 gridview 값을 포함하는 방법

예 : 주어진 링크 https://imageshack.com/i/f0YGXzlvj에서 하나의 파일이 Draft path에 있음을 알 수 있습니다. 그래서 난 당신이 나머지 세포가 비어 있습니다 볼 수 있듯이 또한 내가

 Total number of files : Draft path files 
    File1:- file name of `draft path` file 

[본체 메일의] 포함해야합니다 .. 첨부 파일로 .. 폴더에서 해당 파일을 첨부 할 필요가 ... 그래서 본문에서 언급 할 필요 ..

는 더 많은 파일 경우 내가 그에 따라 할 필요가 없다는 것입니다 ..

코드 스 니펫 : -

private void button2_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Sending Mail. Click Ok!!!", "Mail!!!."); 
     string smtpserver = ini.ReadValue("bmail", "smtpserver"); 
     string email_From = ini.ReadValue("bmail", "email_From"); 
     string email_Recipient = ini.ReadValue("bmail", "email_Recipient"); 
     string email_Subject = ini.ReadValue("bmail", "email_Subject"); 
     string email_Body = ini.ReadValue("bmail", "email_Body"); 


      try 
      { 
       new SmtpClient(smtpserver, 25).Send(email_From, 
             email_Recipient, 
             email_Subject, 
             email_Body); 
       MessageBox.Show("Email Successfully Sent!!!", "Mail!!!."); 
       Environment.Exit(0); 
      } 

      catch (System.Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

어떻게하면 되나요? 도와주세요.

+1

첨부 파일을 추가하거나 경로에서 파일을 검색하는 것이 무엇이 문제입니까? – mybirthname

+0

@mybirthname 첨부 파일을 추가 할 수없고 메일 본문에 파일 이름을 포함하는 방법을 모른다. –

+0

@mybirthname 어떤 아이디어 !!!!! –

답변

1

첨부 파일과 함께 메일을 보낼 수 있습니다.


이메일이 전송되는 동안 현재 스레드를 차단하지 않으려면

SmtpClient smtp = new SmtpClient(); 
MailMessage msg = new MailMessage(); 
msg.From = new MailAddress("[email protected]", "Stacy Kebler"); 
smtp.Host = "smtp.gmail.com"; 
smtp.Port = 465; //set the default smtp port of email provider. you can avoid it if you don't know 
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "stacy123"); 
smtp.EnableSsl = true; //Set this to true if the email provider is using SSL encryption 
smtp.Timeout = 10000; //Set the timeout to 10 second 

msg.To.Add(new MailAddress("[email protected]","Mr. ABC")); 
msg.IsBodyHtml = true; //if the content of body is in HTML format then set it to true. 


msg.Subject = "This is a sample message"; 

StringBuilder sbBody = new StringBuilder(); 
sbBody.Append("This is the Sample Email <br><br>"); 
for (int i = 0; i < dataGridView.Rows.Count; i++) 
{ 
    if (dataGridView.Rows[i].Cells["DRAFT_PATH"].Value != null && 
     System.IO.File.Exists(dataGridView.Rows[i].Cells["DRAFT_PATH"].Value.ToString())) 
    { 
     string path = dataGridView.Rows[i].Cells["DRAFT_PATH"].Value.ToString(); 
     sbBody.AppendFormat("File {0}:{1}<br>", i + 1, Path.GetFileNameWithoutExtension(path)) 
     msg.Attachments.Add(new Attachment(path)); 
    } 
} 
msg.Body = sbBody.ToString(); 


smtp.Send(msg); 

. 비동기 메서드를 사용하여 메일을 보낼 수 있습니다. 전자 메일을 보내는 동안 프로세스가 차단되지는 않습니다. 당신은 대신 두 번째 매개 변수는 해당 프로세스의 토큰입니다 Send()

smtp.SendAsync(msg, "Test Message"); 

SendAsync() 방법을 사용해야합니다. 전자 메일을 보낸 후 추가 프로세스를 수행하고 여러 전자 메일을 보내려는 경우 token은 특정 전자 메일 프로세스를 결정하는 데 도움이됩니다.

예를 들어

: 당신이 다음 전자 메일 서버의 이메일 게이트웨이가 있어야 자격 증명을 지정하지 않고 이메일을 보내려면 동시에

smtp1.SendAsync(msg1, "Test Message 1"); 
smtp1.SendCompleted += new SendCompletedEventHandler(this.SendCompletedCallback); 


smtp2.SendAsync(msg2, "Test Message 2"); 
smtp2.SendCompleted += new SendCompletedEventHandler(this.SendCompletedCallback); 


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

    if (token == "Test Message 1") 
     //This is the First email status 
    else if (token == "Test Message 2") 
     //This is the second email status 
} 

에이 메일을 보내는 경우.

SmtpClient msg = new SmtpClient("username.gateway.com"); 
+0

첨부 파일은 완벽한 방법으로 제공됩니다 ..하지만 그 줄은 필요 없습니다.'smtp.Credentials = new System.Net.NetworkCredential ("[email protected]", "stacy123"); '나는 자격증이 없다는 것을 의미합니다. 어떻게 할 수 있습니까? 제발 내 질문을 참조하십시오 .. 나는 자격증을주고 싶지 않기 때문에 그런 것이 필요합니다. 또한 메일 본문에 파일 이름과 함께주고 싶습니다. 줄 단위로 .. –

+0

예를 들어 .. File1 : - 224-3323_01. File2 : - 121-2322_00 .. 이런 식으로 .. 나는 파일 이름을 추출하고 몸에 넣어 .. .. 또한 ... –

+1

사용자 ID와 비밀번호가 없으면 이메일을 보낼 수 없습니다. 그렇지 않으면 귀하의 계정에서 가짜 이메일을 보낼 수도 있습니다. 그것은 불가능합니다. 그리고 내 대답도 업데이트하고 있습니다. – Shell

관련 문제