-2

我需要一个最佳(有问题的性能)代码来将邮件发送到带有附件文件的多个电子邮件地址并在 c# 中获得交付

4

2 回答 2

3

在 C# 中发送邮件:

使用 SMTP 服务器在 ASP.NET 中发送带附件的电子邮件

MailMessage 有一个DeliveryNotificationOptions 属性,设置如下:

messagetest .DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

于 2012-12-19T07:58:40.307 回答
2

这是从 CSV 文件中读取名称、电子邮件地址的代码,因此它可以发送到这些地址。它涉及许多功能。每一个都很清楚。

//Declare these two variables global in the form, class
MailMessage message;//= new MailMessage()
SmtpClient smtpClient;//= new SmtpClient()

//A function that will read data from CSV File into List and will create a     DataTable 
public DataTable maketable()
    {
        string path = this.Server.MapPath("app_data");
        path += "\\employeelist.csv";
        List<string[]> testParse =
          parseCSV(path);
        DataTable newTable = new DataTable();
        foreach (string column in testParse[0])
        {
            newTable.Columns.Add();
        }

        foreach (string[] row in testParse)
        {
            newTable.Rows.Add(row);
        }
        return newTable;
    }
//This Functions Actually reads the CSV file and make list
public List<string[]> parseCSV(string path)
    {
        List<string[]> parsedData = new List<string[]>();
        try
        {
            using (StreamReader readFile = new StreamReader(path))
            {
                string line;
                string[] row;
                while ((line = readFile.ReadLine()) != null)
                {
                    row = line.Split(',');
                    parsedData.Add(row);
                }
            }
        }
        catch (Exception e)
        {
            parent.list.Items.Add(e.Message);
        }
        return parsedData;
    }

//I have called below function with given subject and body text as a string at any place.. either in button_click event or any other place
SendAttendanceWithAttachment(email_subject, body);
// Now the body of SendAttendanceWithAttachment(.....);
void SendAttendanceWithAttachment(string subject, string body)
    {
        string FileAbsolutePath = "C:\\att\\"; // where the directory is containing files for attachment
        string FileName = "";
        Attachment attach;
        //OPen CSV File Read all contact and then send one by one. 
        smtpClient = new SmtpClient();
        smtpClient.Host = "<smtp host>";//like for google : ssl://smtp.gmail.com
        smtpClient.Port = <int>;// port number for google 465
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("<gmail address>", "<password>");

        MailAddress fromAddress = new MailAddress("<sender email address>");
        dt = maketable();
        try
        {
            add_item("Weekly attendance Sending starts....");
            for (int i = 0; i < dt.Rows.Count; i++)//dt.Rows.Count
            {
                message = new MailMessage();
                if (dt.Rows[i][1].ToString() == string.Empty)
                {
                    continue;
                }
                else
                {
                    FileName = FileAbsolutePath + dt.Rows[i][0].ToString() + ".csv";
                    if (File.Exists(FileName))
                    {
                        attach = new Attachment(FileName);
                        //Add in data
                        message.To.Add(dt.Rows[i][1].ToString());
                        message.Subject = subject;
                        message.IsBodyHtml = true;
                        message.Body = body;
                        message.From = fromAddress;
                        message.Attachments.Add(attach);
                        //add_item("Sending Message " + (i+1) + " of " + dt.Rows.Count + " to : " + dt.Rows[i][0].ToString());
                        SendMail(message, smtpClient);
                        //add_item("Email Sent to :" + dt.Rows[i][0].ToString());
                    }
                }
            }
            //add_item("Weekly attendance Sent to all..");
        }
        catch (System.Exception e)
        {
            //parent.list.Items.Add(e.Message);
        }
//Finally the SendMail()...
public void SendMail(MailMessage message, SmtpClient smtpClient)
    {
        try
        {
            smtpClient.Send(message);
        }
        catch (Exception ex)
        {
            //parent.list.Items.Add(ex.Message);
        }
    }

这是一个完整的过程,您如何从 CSV 文件中读取,将地址添加到列表,然后添加到 DataTable,然后编写带有文件附件的电子邮件并发送。希望你能从中受益。

于 2012-12-19T08:49:56.520 回答