试图发送一个基本的电子邮件到文件夹(trying to send a basic email to

2019-10-17 14:49发布

我试图发送一个基本的电子邮件给folderbut howerver,即使我做接收电子邮件,身体完全丢失。

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年完成的感觉。 任何意见吧?

Answer 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) { }
}

有关更多信息,请发送邮件/联系表使用ASP.NET和C#

我希望这会帮助你。



Answer 2:

似乎你缺少的:

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

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

msg.Body属性为空在你的榜样,我想这就是为什么它不发送正文。



文章来源: trying to send a basic email to folder