我在SQL中有一个简单的表(MyEmail),需要发送一些电子邮件,例如:
ID Email
1 [email protected]
2 [email protected]
3 [email protected]
4 [email protected]
我创建了一个存储过程(GetAddress)来收集它们,以便稍后将它们存储到变量中:
SELECT Email
FROM dbo.MyEmai
我需要C#部分代码的帮助:
var MyEmails = new List<Email>();
SqlCommand cmdEmails = new SqlCommand("GetAddress", connection);
SqlDataReader rdEmails = cmdEmails.ExecuteReader();
while (rdEmails.Read())
{
MyEmails.Add(new Email() { MyEmails = rdEmails[0].ToString() }); // as an example
}
此代码返回列表,但电子邮件位于Web Application.MyPage下面。电邮姓名。我的电子邮件回复:
WebApplication.MyPage.Email > [email protected]
WebApplication.MyPage.Email > [email protected] ...
我需要删除此WebApplication.MyPage.Email,因此只有电子邮件将首先显示为字符串。
发送电子邮件的代码:
SmtpClient client = new SmtpClient();
client.Port = 112;
client.Host = "my-smtp";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "");
MailMessage mm = new MailMessage(LocalName, LocalName + MyEmails, "New Mail subject", "This is Email body !");
client.Send(mm);
所以因为这个MyEmails有一个错误:错误24参数2:无法从'System.Collections.Generic.List'转换为'string'
有人可以帮我弄这个吗?
提前致谢!
.Net的MailMessage类不接受List作为有效参数。对你的收藏进行抄袭,创建多个mailmessage。
代码看起来应该是这样的
foreach (var emailadressObject in myEmails)
{
// get your emailadres string from your object..
// Bit confusion using a collection MyEmails and a Property in you mail objetc with MyEmails
var emailadresstring = emailadressObject.MyEmails;
var message = new MailMessage("[email protected]", emailadresstring, "New Mail subject", "This is Email body !");
// Do you magic with the mail message
}