尝试将基本电子邮件发送到文件夹

时间:2013-01-16 11:55:29

标签: c# visual-studio-2008 html-email

我正在尝试向一个文件夹发送一个基本的电子邮件,但即使我收到了电子邮件,也完全没有丢失正文。

private void MailReport()
{

    string to = "arianul@gmail.com";
    string From = "ArianG@lr.co.za";
    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="ArianG@lr.co.za">
    <network host="staging.itmaniax.co.za"/>
    <specifiedPickupDirectory pickupDirectoryLocation="C:\testdump\emaildump\"/>
  </smtp>
</mailSettings>

我觉得问题在于身体和html,因为它是通过visual studio-2008完成的。 或许有任何建议吗?

2 个答案:

答案 0 :(得分:1)

似乎你错过了:

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

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

msg.Body属性在你的例子中是空的,我猜这就是它不发送正文的原因。

答案 1 :(得分:1)

您可以尝试以下内容。

protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // 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#

我希望这会对你有所帮助。