【问题标题】:Sending mail through automation (c # ) [mail with attachement]通过自动化发送邮件(c#)[带附件的邮件]
【发布时间】:2019-09-19 18:35:50
【问题描述】:

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

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

现在,我想通过邮件将这些报告发送给客户 自动化。

如何配置这些设置以及需要什么?

所有生成的报告都应交付给客户。

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

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

谢谢你..

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    这不仅仅是一个 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);
    }
    

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

    【讨论】:

    • 这个。您可能只是想通过电子邮件发送文件。 You may as well use this.
    • 我不能用这个附加文件吗?
    【解决方案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了解更多信息。

    谢谢你..

    【讨论】:

    • 这确实是一种方法。将报告作为附件附加到您的消息中,或者根据您的报告内容构建消息正文。
    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多