2015-10-31 21 views
0

我知道这个问题已经存在,但我阅读了所有这些问题,但没有找到答案。这是我的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); 
     } 
    } 
    } 

我有一个电子邮件 地址

我不知道什么可能是错误的所要求的形式问题

此错误的指定的字符串不是。请帮忙吗?

+0

好吧!你的“SendEmail”不是一个类! – deeiip

+0

“postEmail.emailTo”中的值是否为有效的电子邮件地址?你是否指定了“发件人”地址? –

+0

错误信息不能更清楚。 – jsanalytics

回答

0

把一个破发点就行了

message.To.Add(new MailAddress(postEmail.emailTo)); 

,当调试器到达该线时运行代码 检查电子邮件地址的值 postEmail.emailTo

其最有可能的格式错误,这就是产生错误的原因。

0

这是定义客户端和发送电子邮件的正确方法。完整结构的方法是定义是错误的,它不只是emailTo串

命名空间App.MYEmailApp.Service {

公共类EmailService:IEmailService {

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("[email protected]"); 
    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()); 
    } 




} 

}

公开课PostEmail {

public string emailTo; 
public string emailToName; 
public string subject; 
public string body; 
public string emailFrom; 
public string emailFromName; 
public bool isBodyHtml; 

}

}

相关问题