如何在发送邮件时将gridview值包含为附件

时间:2014-09-29 19:50:29

标签: c# email email-attachments

我想从第二列[DRAFT PATH]添加文件名,并在发送邮件时包含为附件。 如何将此详细信息添加为附件,我需要在我的身体中包含文本信息。

代表; - 在给定的链接https://imageshack.com/i/f0YGXzlvj中,您可以看到Draft path中只有一个文件。所以我需要从文件夹中附加该文件..作为附件.. 我还需要包括

[邮件正文]

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

正如你所看到的,剩下的细胞是空的......所以不需要在Body中提及..

如果有更多文件,那么我需要按照那样做..

代码段: -

 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 个答案:

答案 0 :(得分:1)

您可以使用附件发送此类邮件。

SmtpClient smtp = new SmtpClient();
MailMessage msg = new MailMessage();
msg.From = new MailAddress("stacy@gmail.com", "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("stacy@gmail.com", "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("abc@gmail.com","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");