提交Asp.net Windows窗体时发送某种形式的通知

时间:2015-10-28 13:00:17

标签: c# asp.net forms

我有一个asp.net网页表单,点击按钮会显示一个包含3个字段和提交按钮的模式。

我想要的只是通过表格的详细信息向我发送某种通知。

问题是,我的应用程序是否位于我们的DEV' DEV'环境,我正在考虑将电子邮件发送到我们的“Live'环境,所以它需要通过Microsoft Outlook完成,但我无法弄清楚如何做到这一点。

我过去使用过' Hotmail'但我无法弄清楚或收到发送给我的电子邮件。

我试过以下

using Microsoft.Office.Interop.Outlook;

protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
    MailMessage message = new MailMessage();
    message.From = new MailAddress("antonydev@dev1.test.com");

    message.To.Add(new MailAddress("antony@test.co.uk"));
    message.Subject = "This is my subject";
    message.Body = "This is the content";

    SmtpClient client = new SmtpClient();

    client.Send(message);
}

我也尝试过用于hotmail的代码

using System.Net.Mail;
using System.Net;
using System.IO;

protected void BtnSuggestPlace_Click(object sender, EventArgs e)
        {
                //Creates the email object to be sent
                MailMessage msg = new MailMessage();

                //Adds your email address to the recipients
                msg.To.Add("antony@test.co.uk");

                //Configures the address you are sending the email from
                MailAddress address = new MailAddress("antonydev@dev1.test.com");
                msg.From = address;

                //Allows HTML to be used when setting up the email body
                msg.IsBodyHtml = true;

                //Email subjects title
                msg.Subject = "Place Suggestion";

                msg.Body = "<b>" + lblPlace.Text + "</b>" + "&nbsp;" + fldPlace.ToString()
                           + Environment.NewLine.ToString() +
                           "<b>" + lblLocation.Text + "</b>" + "&nbsp;" + fldLocation.ToString()
                            + Environment.NewLine.ToString() +
                           "<b>" + lblName.Text + "</b>" + "&nbsp;" + fldName.ToString();

                //Configures the SmtpClient to send the mail
**NOT TO SURE IF REQUIRED OR NOT**
                    SmtpClient client = new SmtpClient("smtp.live.com", 587);
                    client.EnableSsl = true; //only enable this if the provider requires it

                    //Setup credentials to login to the sender email address ("UserName", "Password")
**NO IDEA WHAT TO PUT HERE AS PASSWORDS EXPIRE EVERY 28DAYS**
                        NetworkCredential credentials = new NetworkCredential("antonydev@dev1.testcom", "CurrentPassword");
                        client.Credentials = credentials;

                    //Send the email
                    client.Send(msg);

                // Create a Outlook Application and connect to outlook 
                Application OutlookApplication = new Application();

                // create the MailItem which we want to send 
                MailItem email = (MailItem)OutlookApplication.CreateItem(OlItemType.olMailItem);
                // Add a recipient for the email
                email.Recipients.Add("antonydev@dev1.test.com");
                // add subject and body of the email
                email.Subject = "Test";
                email.Body = "This is a test email to check outlook email sending code";

                //send email
                email.Send();
}

我所需要的只是传递给我的内容,但不确定如何或以何种方式进行操作

更新

我决定设置一个新的gmail acc并使用它,但它总是落入我的catch我怀疑它的超时可以让任何人帮忙。

InnerException消息是:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应173.194.67.109:587

protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
    #region Email
    try
    {
        //Creates the email object to be sent
        MailMessage msg = new MailMessage();

        //Adds your email address to the recipients
        msg.To.Add("antony@test.co.uk");

        //Configures the address you are sending the email from
        MailAddress address = new MailAddress("NewGmailEmailAddress@gmail.com");
        msg.From = address;

        //Allows HTML to be used when setting up the email body
        msg.IsBodyHtml = true;

        //Email subjects title
        msg.Subject = "Place Suggestion";

        msg.Body = "<b>" + lblPlace.Text + "</b>" + "&nbsp;" + fldPlace.Text
                   + Environment.NewLine.ToString() +
                   "<b>" + lblLocation.Text + "</b>" + "&nbsp;" + fldLocation.Text
                    + Environment.NewLine.ToString() +
                   "<b>" + lblName.Text + "</b>" + "&nbsp;" + fldName.Text;

        SmtpClient client = new SmtpClient("smtp.gmail.com");
        NetworkCredential nc = new NetworkCredential("XXXusername", "MyPassword");//username doesn't include @gmail.com
        client.UseDefaultCredentials = false;
        client.Credentials = nc;
        client.EnableSsl = true;
        client.Port = 587;

        //Send the email
        client.Send(msg);
    }
    catch
    {
        //Lets the user know if the email has failed
        lblNotSent.Text = "<div class=\"row\">" + "<div class=\"col-sm-12\">" + "There was a problem sending your suggestion. Please try again." + "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-sm-12\">" + "If the error persists, please contact Antony." + "</div>" + "</div>";
    }
    #endregion
}

3 个答案:

答案 0 :(得分:0)

以下是我用于使用Gmail帐户发送SMTP邮件的一些代码:

    public static void Send(string subject, string message, params string[] recipients)
    {
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential([YourAccountName], [YourAccountPassword]);//username doesn't include @gmail.com
        client.UseDefaultCredentials = false;
        client.Credentials = nc;
        client.EnableSsl = true;
        client.Port = 587;
        try
        {
            string recipientString = string.Join(",", recipients);
            client.SendAsync([YourAccountName] + "@gmail.com", recipientString, subject, message, null);
        }
        catch (Exception ex)
        {
            Logger.Report(ex);
        }
    }

答案 1 :(得分:0)

173.194.67.109是有效的gmail IP:不指定端口以便与SSL建立smtp连接(587TLS端口)。

我还建议使用web.config来存储smtp设置:

<appSettings>
    <add key="enableSSL" value="true"/>
</appSettings>

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="yourgmail@gmail.com">
            <network
                 host="smtp.gmail.com"
                 userName="yourgmail@gmail.com"
                 password="YourPassword" />
        </smtp>
    </mailSettings>
</system.net>

所以你的代码可能是这样的:

Dim email As New System.Net.Mail.MailMessage()
email.Body = "YourBody"
email.Subject = "YourSubject"
email.To.Add("yourrecipient@email.com")

Dim smtp As New System.Net.Mail.SmtpClient()
smtp.EnableSsl = System.Configuration.ConfigurationManager.AppSettings("enableSSL")
smtp.Send(email)

答案 2 :(得分:0)

事实证明防火墙正在阻止它,所以问题不在于代码本身。