2013-01-16 37 views
1

我想發送一個基本的電子郵件到一個文件夾,但howerver,即使我收到電子郵件,身體是完全缺失。試圖發送一個基本的電子郵件到文件夾

private void MailReport() 
{ 

    string to = "[email protected]"; 
    string From = "[email protected]"; 
    string subject = "Report"; 
    **string Body = "Dear sir ,<br> Plz Check d Attachment <br><br>";** 


    bool send = sendMail(to, From, subject, Body); 

    if (send == true) 
    { 
     string CloseWindow = "alert('Mail Sent Successfully!');"; 
     ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true); 
    } 
    else 
    { 
     string CloseWindow = "alert('Problem in Sending mail...try later!');"; 
     ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true); 
    } 
} 

public bool sendMail(string to, string from, string subject, string body) 
{ 
    bool k = false; 
    try 
    { 
     MailMessage msg = new MailMessage(from, to); 
     msg.Subject = subject; 

     AlternateView view; 
     SmtpClient client; 
     StringBuilder msgText = new StringBuilder(); 
     view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html"); 
     msg.AlternateViews.Add(view); 


     msgText.Append("<body><br></body></html> <br><br><br> " + body); 

     client = new SmtpClient(); 
     client.Host = "staging.itmaniax.co.za"; 
     client.Send(msg); 

     k = true; 

    } 
    catch (Exception exe) 
    { 
     Console.WriteLine(exe.ToString()); 
    } 
    return k; 


} 

<system.net> 
<mailSettings > 
    <smtp deliveryMethod="specifiedPickupDirectory" from="[email protected]"> 
    <network host="staging.itmaniax.co.za"/> 
    <specifiedPickupDirectory pickupDirectoryLocation="C:\testdump\emaildump\"/> 
    </smtp> 
</mailSettings> 

我有感覺問題出在身體和HTML,因爲它是與Visual Studio 2008年完成。 也許有什麼建議?

+1

將斷點放入'sendMail'方法中,確保'body'有一個值。 msgText的HTML純屬錯誤。我會去掉HTML,只是轉儲到正文中,看看它是否通過OK。 – Arran

回答

1

你可以嘗試下面的東西。

protected void SendMail() 
{ 
    // Gmail Address from where you send the mail 
    var fromAddress = "[email protected]"; 
    // any address where the email will be sending 
    var toAddress = YourEmail.Text.ToString(); 
    //Password of your gmail address 
    const string fromPassword = "Password"; 
    // Passing the values and make a email formate to display 
    string subject = YourSubject.Text.ToString(); 
    string body = "From: " + YourName.Text + "\n"; 
    body += "Email: " + YourEmail.Text + "\n"; 
    body += "Subject: " + YourSubject.Text + "\n"; 
    body += "Question: \n" + Comments.Text + "\n"; 
    // smtp settings 
    var smtp = new System.Net.Mail.SmtpClient(); 
    { 
     smtp.Host = "smtp.gmail.com"; 
     smtp.Port = 587; 
     smtp.EnableSsl = true; 
     smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
     smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
     smtp.Timeout = 20000; 
    } 
    // Passing values to smtp object 
    smtp.Send(fromAddress, toAddress, subject, body); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     //here on button click what will done 
     SendMail(); 
     DisplayMessage.Text = "Your Comments after sending the mail"; 
     DisplayMessage.Visible = true; 
     YourSubject.Text = ""; 
     YourEmail.Text = ""; 
     YourName.Text = ""; 
     Comments.Text = ""; 
    } 
    catch (Exception) { } 
} 

有關更多信息,請Send Mail/Contact Form using ASP.NET and C#

我希望這會幫助你。

1

似乎你缺少的:

MailMessage msg = new MailMessage(from, to); 
msg.Subject = subject; 

msg.Body = body; <--- try adding this and see what happens. 

msg.Body屬性爲空,在你的榜樣,我想這就是爲什麼它不發送正文。

相關問題