此SELECT返回项目所有者的电子邮件地址:
var selectEmailData = "SELECT User.Email FROM Projects INNER JOIN User ON Projects.ProjectOwner = User.UserId WHERE ProjectId=@0";
var dataEmail = db.Query(selectEmailData, ProjectId);
@foreach (var row in dataEmail)
{
@row.Email
}
但是当我尝试这个时:
var mailTo = dataEmail.ToString();
发送电子邮件:
WebMail.Send(
to: mailTo,
subject: "Hello there!",
body: "Your project is ready."
);
我得到了这个:指定的字符串不是电子邮件地址所需的格式。
这段代码有什么问题?
答案 0 :(得分:0)
我很确定......
var dataEMail = db.Query(…) would return a collection not a string.
... SO
var mailTo = dataEmail.ToString();
将返回类似" System.Collections.Generic ......"
的内容我猜你可能想要这样的东西
var selectEmailData = "SELECT User.Email FROM Projects INNER JOIN User ON Projects.ProjectOwner = User.UserId WHERE ProjectId=@0";
var allEmails = db.Query(selectEmailData, ProjectId);
@foreach (var row in allEmails)
{
WebMail.Send(
to: @row.Email,
subject: "Hello there!",
body: "Your project is ready."
)
答案 1 :(得分:0)
验证
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
{
//Send email
}
else
{
//Bad email
}
其他方式:
try
{
MailAdress email = new MailAddress(<enter here string>);
}
catch (FormatException fe)
{
//Bad email
}