2013-12-13 23 views
0

我已創建了一個mail.aspx.cs與下面的代碼在client.send(消息)上發送郵件時出錯?

protected void Page_Load(object sender, EventArgs e) 
    { 
     MailMessage message = new MailMessage(); 
     message.From = new MailAddress("[email protected]"); 
     message.To.Add(new MailAddress("[email protected]")); 
     message.Subject = "This is my subject"; 
     message.Body = "This is the content"; 
     SmtpClient client = new SmtpClient(); 
     client.Send(message); 
    } 

而且web.config設置

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="smtpserver1" port="25" userName="abc xyz" password="abc" defaultCredentials="true" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 

獲取失敗錯誤,而在下面的行

client.Send(message); mail sending fail, 
在頁面加載發送郵件

我是新來的asp.net,指導我解決這個問題的正確方法。

+0

當你發佈任何問題。不要發佈您的原始用戶名密碼,任何機構都可能會濫用它。請用虛擬用戶名和密碼替換它。 – Raghubar

+0

不是解決方案,但是您應該使用'使用(MailMessage消息=新MailMessage()){...使用(SmtpClient客戶端=新SmtpClient()){...}}' –

回答

0
message.From = new MailAddress("[email protected]"); 

而且

<mailSettings> 
     <smtp from="[email protected]"> 

必須是相同的!

+0

也刪除此行中的空格:<網絡主機=「smtpserver1」端口=「25」用戶名=「NITIN TURANKAR」密碼=「mkoZAQ!@#$%()」defaultCredentials =「true」/> – desmati

0

您正在使用的用戶名與空的空間userName="NITIN TURANKAR",這是錯誤的

+0

意味着空間不允許? –

+0

是AFAIK用戶名不能包含空格。 – CloudyMarble

0
public string SendMail(string toList, string from, string ccList, string subject, string body) 
{ 

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
    SmtpClient smtpClient = new SmtpClient(); 
    string msg = string.Empty; 
    try 
    { 
     MailAddress fromAddress = new MailAddress(from); 
     message.From = fromAddress; 
     message.To.Add(toList); 
     //if (attachments != "") 
     //{ 
     // message.Attachments.Add(new Attachment(attachments)); 
     //} 
     if (ccList != null && ccList != string.Empty) 
      message.Bcc.Add(ccList); 
     message.Subject = subject; 
     message.IsBodyHtml = true; 
     message.Body = body; 
     // We use gmail as our smtp client 
     smtpClient.Host = "smtp.gmail.com"; 
     smtpClient.Port = 25; 
     smtpClient.EnableSsl = false; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = new System.Net.NetworkCredential(
      "Email", "password"); 

     smtpClient.Send(message); 
     msg = "Successful<BR>"; 

     string message1 = "Your Query has been Submited Successfully"; 

    } 
    catch (Exception ex) 
    { 

    } 
    return msg; 
} 

嘗試端口替換爲587同樣。