指定的字符串不是电子邮件地址所需的格式,不知道有什么问题?
The specified string is not in the form required for an e-mail address, no Idea what is wrong?
我知道这个问题已经存在,但我阅读了所有问题,但没有找到答案。这是我的 SendEmail 方法。
public bool SendEmail(PostEmail postEmail)
{
if (string.IsNullOrEmpty(postEmail.emailTo))
{
return false;
}
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
message.Subject = postEmail.subject == null ? "" : postEmail.subject;
message.Body = postEmail.body == null ? "" : postEmail.body;
message.IsBodyHtml = postEmail.isBodyHtml;
message.To.Add(new MailAddress(postEmail.emailTo));
try
{
smtpClient.Send(message);
return true;
}
catch (Exception exception)
{
//Log the exception to DB
throw new FaultException(exception.Message);
}
}
}
我遇到了这个错误
The specified string is not in the form required for an e-mail
address
我不知道哪里出了问题。请任何帮助?
在线打个断点
message.To.Add(new MailAddress(postEmail.emailTo));
并且当您 运行 代码时调试器命中该行
检查电子邮件地址的值
postEmail.emailTo
它的格式很可能是错误的,这就是生成的格式
错误。
这是定义客户端和发送电子邮件的正确方法。定义的完整结构是错误的,它不仅仅是关于 emailTo string
命名空间App.MYEmailApp.Service
{
public class 电子邮件服务:IEmail服务
{
public void SendEmail(PostEmail postEmail)
{
MailAddress from = new MailAddress(postEmail.emailFrom, postEmail.emailFromName);
MailAddress to = new MailAddress(postEmail.emailTo, postEmail.emailToName);
MailMessage message = new MailMessage(from, to);
message.Subject = postEmail.subject;
message.Body = postEmail.body;
MailAddress bcc = new MailAddress("xxxx@gmail.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient();
//client.UseDefaultCredentials = false;
//client.Credentials.GetCredential("smtp.xxxx.com", 587, "server requires authentication");
Console.WriteLine("Sending an e-mail message to {0} and {1}.", to.DisplayName, message.Bcc.ToString());
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
}
public class 邮箱
{
public string emailTo;
public string emailToName;
public string subject;
public string body;
public string emailFrom;
public string emailFromName;
public bool isBodyHtml;
}
}
我知道这个问题已经存在,但我阅读了所有问题,但没有找到答案。这是我的 SendEmail 方法。
public bool SendEmail(PostEmail postEmail)
{
if (string.IsNullOrEmpty(postEmail.emailTo))
{
return false;
}
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
message.Subject = postEmail.subject == null ? "" : postEmail.subject;
message.Body = postEmail.body == null ? "" : postEmail.body;
message.IsBodyHtml = postEmail.isBodyHtml;
message.To.Add(new MailAddress(postEmail.emailTo));
try
{
smtpClient.Send(message);
return true;
}
catch (Exception exception)
{
//Log the exception to DB
throw new FaultException(exception.Message);
}
}
}
我遇到了这个错误
The specified string is not in the form required for an e-mail address
我不知道哪里出了问题。请任何帮助?
在线打个断点
message.To.Add(new MailAddress(postEmail.emailTo));
并且当您 运行 代码时调试器命中该行 检查电子邮件地址的值 postEmail.emailTo
它的格式很可能是错误的,这就是生成的格式 错误。
这是定义客户端和发送电子邮件的正确方法。定义的完整结构是错误的,它不仅仅是关于 emailTo string
命名空间App.MYEmailApp.Service {
public class 电子邮件服务:IEmail服务 {
public void SendEmail(PostEmail postEmail)
{
MailAddress from = new MailAddress(postEmail.emailFrom, postEmail.emailFromName);
MailAddress to = new MailAddress(postEmail.emailTo, postEmail.emailToName);
MailMessage message = new MailMessage(from, to);
message.Subject = postEmail.subject;
message.Body = postEmail.body;
MailAddress bcc = new MailAddress("xxxx@gmail.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient();
//client.UseDefaultCredentials = false;
//client.Credentials.GetCredential("smtp.xxxx.com", 587, "server requires authentication");
Console.WriteLine("Sending an e-mail message to {0} and {1}.", to.DisplayName, message.Bcc.ToString());
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
}
public class 邮箱 {
public string emailTo;
public string emailToName;
public string subject;
public string body;
public string emailFrom;
public string emailFromName;
public bool isBodyHtml;
}
}