通过自动化发送邮件(c#)[带附件的邮件]

时间:2012-05-28 09:37:12

标签: c# selenium

我在visual studio(c#)中使用selenium RC运行了一些录制的脚本。

我很容易报告这些脚本。(我将所有结果保存在文本文件中)

现在,我希望通过邮件的形式将这些报告发送给客户 自动化。

如何配置这些设置以及所需的所有内容?

生成的所有报告都应该发送给客户。

建议存在示例的网站或链接。

还提供有关配置和设置的步骤。

谢谢..

2 个答案:

答案 0 :(得分:4)

这比基于Selenium的问题更基于C#。

整个网站都致力于详细解释如何使用C#和System.Net.Mail命名空间发送电子邮件:

http://www.systemnetmail.com/

一个简单的例子:

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
string fromPassword = "fromPassword";
string subject = "Subject";
string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

您需要做的就是通过阅读您提到的“报告”的内容来构建邮件正文。

答案 1 :(得分:2)

感谢您的代码。

我找到了一些代码来发送附带附件的电子邮件。

using System.Net;
using System.Net.Mail;

public void email_send()
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("your mail@gmail.com");
        mail.To.Add("to_mail@gmail.com");
        mail.Subject = "Test Mail - 1";
        mail.Body = "mail with attachment";

        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
        mail.Attachments.Add(attachment);

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

    }

阅读Sending email using SmtpClient了解详情。

谢谢..