错误无法将类型'string'隐式转换为System.Net.Mail.MailAddressCollection [英] error cannot implicitly convert type 'string' to System.Net.Mail.MailAddressCollection

查看:141
本文介绍了错误无法将类型'string'隐式转换为System.Net.Mail.MailAddressCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i在c#和asp.net工作

i希望发送邮件给某些人

我使用类和方法如下代码:



hi i work in c# and asp.net
i want to sent mail to some people
and i use class and method like below code:

using System.Net.Mail;

public void sendmail(string Subject, string ToEmail, string Body)
{
     SmtpClient MyMail = new SmtpClient();
     MailMessage MyMsg = new MailMessage();
     MyMail.Host = "webmail.tahasoft.net";

     MyMsg.To = ToEmail;

     MyMsg.Subject = Subject;
     MyMsg.SubjectEncoding = Encoding.UTF8;
     MyMsg.IsBodyHtml = true;
     MyMsg.From = new MailAddress("info@mysite.net", "mysite");
     MyMsg.BodyEncoding = Encoding.UTF8;
     MyMsg.Body = Body;
     MyMail.UseDefaultCredentials = false;
     NetworkCredential MyCredentials = new NetworkCredential("info@mysite.net", "mysite");
     MyMail.Credentials = MyCredentials;
     MyMail.Send(MyMsg);
}





当我向一个人发送一封电子邮件时它工作正常

但是当我发送ToEmail喜欢:

aaa@yahoo.com; sss@yahoo.com; ddd@yahoo.com



i得到错误:

错误无法将类型''string''隐式转换为System.Net.Mail.MailAddressCollection



when i sent one email to one person it works fine
but when i sent ToEmail like to:
aaa@yahoo.com;sss@yahoo.com;ddd@yahoo.com

i get error:
error cannot implicitly convert type ''string'' to System.Net.Mail.MailAddressCollection

推荐答案

您需要调用Add方法列表中的每个地址,您需要将它们转换为MailAddress对象:

You need to call the Add method for each address in your list, and you need to convert them to MailAddress objects:
mail.To.Add(new MailAddress(to));



在您的情况下,您可能希望将名称拆分为单独的字符串,然后添加它们:


In your case, you probably want to split the names into separate strings, and add them:

string[] tos = ToEmail.Split(';');
foreach (string to in tos)
   {
   mail.To.Add(new MailAddress(to));
   }


拆分字符串上的字符串'';''

Split the string on the char '';''
string[] Addresses = s.Split(';');



然后:


and then:

MailMessage msg = new MailMessage();
foreach (string address in Addresses)
   msg.To.Add(address);





干杯,

江户



Cheers,
Edo


用户以下代码向多个收件人发送电子邮件,

User following code to send email to multiple recipients,
MyMsg.To.Add(new System.Net.Mail.MailAddress(ToEmail));


这篇关于错误无法将类型'string'隐式转换为System.Net.Mail.MailAddressCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆