我想添加第2列[DRAFT PATH
]中的文件名,并在发送邮件时作为附件包含。我如何将这些详细信息作为附件添加,并且我需要在正文中包含文本信息。
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);
}
}
我该怎么做。。请帮帮我。
您可以通过附件发送这样的邮件。
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");