尝试在ASP中发送包含不同电子邮件地址的电子邮件

时间:2016-11-29 01:36:33

标签: c# asp.net sql-server smtpclient

我正在尝试发送电子邮件,电子邮件地址来自SQL DB

我的数据库中的数据示例是:

email1@mail.com, email2@mail.com

但是当我通过我的ASP.NET应用程序发送电子邮件时,它只会发送到email2@mail.com

数据类型为nvarchar(max)

我该如何解决这个问题?谢谢!

更新

public void emailtoPIC()
    {


        SmtpClient smtpClient = new SmtpClient();

        NetworkCredential NetworkCred = new NetworkCredential("online.****@ph.yusen-logistics.com", "****");
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = NetworkCred;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.Host = "17*.*.*.**";
        smtpClient.Port = **;
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("online.****@ph.yusen-logistics.com", "****Online Travel Request");
        mail.CC.Add(new MailAddress(hidePICemail.Text));

        mail.Subject = "[Travel Order]" + TravelNumber.Text + " - For Processing";
        mail.Body = "<html>" +
            "<body>" +
            "<style>" +
            "p {" +
            "font-size:15px;" +
            "color:black;" +
            "font-family: Calibri" +
            "}" +
            "</style>" +
            "<p>Greetings </p>" +
            "<p>You have a Travel Request for Processing</p>" +
            "<p>Travel Request Number: <b>" + TravelNumber.Text + "</b></p>" +
            "<p> Date/Time Filed: <b>" + DateValue.Text + "</b></p>" +
            "<p>Please see below details </p>" +
            "<hr align='left' width='50%'>" +
            "<p>Name of Traveler: <b>" + txtName.Text + "</b></p>" +
            "<p>Business Unit: <b>" + ddlBU.SelectedItem.Text + "</b></p>" +
            "<p>Department: <b>" + ddlDept.SelectedItem.Text + "</b></p>" +
            "<p>Type of Travel: <b>" + ddlType.SelectedItem.Text + "</b></p>" +
            "<p>Destination: <b>" + txtDesti.Text + "</b></p>" +
            "<p>Purpose of Travel: <b>" + ddlPurpose.SelectedItem.Text + "</b></p>" +
            "<p>Details: <b>" + txtDetails.Text + "</b></p>" +
            "<p>Preferred Departure: <b>" + txtDepDate.Text + "</b></p>" +
            "<p>Preffered Return: <b>" + txtRetDate.Text + "</b></p>" +
            "<p>Flight Time (ETD / ETA): <b>" + txtFlight.Text + "</b></p>" +
            "<p>Booking PIC: <b>" + txtPIC.Text + "</b></p>" +
            "<p>Remarks: <b>" + txtRemarks.Text + "</b></p>" +
            "<p>Approver 1: <b>" + ddlapp1.SelectedItem.Text + "</b></p>" +
            "<p>Approver 2: <b>" + ddlapp2.SelectedItem.Text + "</b></p>" +
            "<p>Status: <b>" + txtStatus.Text + " </b></p>" +
            "<p><a href ='http://172.20.3.24/Yusen/TOR/ProcessRequests.aspx'>Click here to process the request</a></p>" +
            "</body>" +
            "</html>";

        mail.IsBodyHtml = true;

        try
        {
            smtpClient.Send(mail);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;

            }
            Response.Write("<script>alert('Sending failed..')</script>");
        }
    }

hidePICEmail 来自我的数据库,我将多个电子邮件存储在一个列中。

1 个答案:

答案 0 :(得分:1)

您需要将每个To电子邮件地址添加到MailMessage。你可以这样做。

    List<string> emailTo = new List<string>();
                    emailTo.AddRange(receiverEmail.Split(',').ToList<string>()); // Create a list of To Addresses
                    using (MailMessage message = new MailMessage())
                    {
                        message.From = new MailAddress(emailFrom);
                        // Add each to address 
                        foreach (string EmailId in emailTo)
                        {
                            message.To.Add(new MailAddress(EmailId));
 //message.CC.Add(new MailAddress(EmailId)); // You can do the same thing for CC email
                        }

                        message.Subject = emailSubject;
                        message.IsBodyHtml = true;
                        message.Body = messageBody;
                        SmtpClient client = new SmtpClient();
                        client.Host = smtpHost;
                        client.Port = Convert.ToInt32(smtpPort);
                        client.Send(message);
                    }