首页 > 解决方案 > 如何在mailbee LoadMessage的路径中进行连接

问题描述

这是我用来从带有扩展名的文件夹中获取所有名称的代码

string[] files = Directory.GetFiles(@"D:\DLogs\Notification11");
foreach (string file in files)
{

    //Console.WriteLine(Path.GetFileName(file));
    listOfFiles.Add(Path.GetFileName(file));
    mailBeeTask(listOfFiles);
}

现在的问题是,在 mailBeeTask(listOfFiles) 我给出的文件名带有扩展名,但 mailbee 使用

mailer.Message.LoadMessage(@"D:\DLogs\Notification11\mailbee.eml");

这是mailbee代码

public static void mailBeeTask(IList<string> ListOfTasks)
{
    //send emails
    Smtp mailer = new Smtp();

    // Use SMTP relay server with authentication.
    mailer.SmtpServers.Add("smtp.domain.com", "joe@domain.com", 
    "secret");

    // Load the message from .EML file (it must be in MIME RFC2822 
    format).
    mailer.Message.LoadMessage(@"D:\DLogs\Notification11\mailbee.eml");
    **//this above line is the problem, how can i use ListOfTasks 
    instead 
    //of mailbee.eml should i concatenate or what??**

    // Demonstrate that we can modify the loaded message.
    // Update the date when the message was composed to the current 
    moment.
    mailer.Message.Date = DateTime.Now;

    mailer.Send();
}

Mailbee 用于发送 afterlogic 制作的电子邮件。

标签: c#email

解决方案


根据逻辑,您希望发送给定目录中的所有消息文件。因为单个文件本身就是一条完整的消息,所以您必须单独发送每个文件/消息。

您正确获取了所有文件。但是你必须mailBeeTask为每个文件执行。

所以 for each 中的两行会得到

mailBeeTask(file);

并且签名mailBeeTask必须更改为

public static void mailBeeTask(string filename)

其次是使用上次更改中的参数

mailer.Message.LoadMessage(filename);

您的代码现在将遍历指定目录中的所有文件,然后将mailBeeTask使用完整文件名作为参数调用该方法。然后该方法加载单个文件,修改日期并发送它。

使用 OP 中的给定代码,您还将遭受以下痛苦:您首先将文件添加到列表中listOfFiles,然后mailBeeTask为列表中的每个文件执行。在下一次迭代中,这会导致列表中的所有先前文件再次作为参数提供。使用已经工作的代码,您将在一次迭代中发送所有先前的文件和当前文件。


推荐阅读