2014-09-29 82 views
1

我想添加第2列[DRAFT PATH]中的文件名,並在發送郵件時將其作爲附件添加。 我如何添加這些細節作爲附件,我需要在我的身體中包含文本信息。如何在發送郵件時包含gridview值作爲附件

適用於Ex; - 在給定的鏈接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 2014-09-29 20:19:05

+0

@mybirthname我無法添加附件,也不知道如何將文件名包含到郵件正文中。 – 2014-09-29 20:45:00

+0

@mybirthname任何想法!!!!! – 2014-09-29 20:57:54

回答

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); 

,如果你不希望在電子郵件發送給阻止當前線程。那麼你可以使用異步方法發送郵件。這不會阻止電子郵件發送過程。你只需要使用SendAsync()方法,而不是Send()

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

當第二個參數是這一進程的令牌。如果您想在發送電子郵件後進行任何進一步處理,並且您還發送多封電子郵件,那麼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」); '我的意思是憑證出來..我該怎麼做。請看我的問題..我需要這樣的東西..因爲我不想給憑據。另外,我想在郵件的正文中給出..與文件名..像行一樣.. – 2014-09-30 03:51:43

+0

例如.. File1: - 224-3323_01。文件2: - 121-2322_00 ..這樣..我想提取文件名並放在身體..也... – 2014-09-30 03:52:30

+1

沒有用戶名和密碼您不能發送電子郵件,否則我也可以從您的帳戶發送假電子郵件。這不可能。我也在更新我的答案。 – Shell 2014-09-30 03:59:52

相關問題