2015-10-31 42 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; 

}

}

相關問題