2015-10-28 23 views
0

我有一個asp.net網頁表單,點擊一個按鈕顯示一個模式,3個字段和一個提交按鈕。發送某種形式的通知當一個Asp.net Windows窗體被提交

我想要的是通過表單的詳細信息向我發送某種通知。

問題是,我的應用程序是否位於我們的'DEV'環境中,並且我正在考慮將電子郵件發送到我們的'實時'環境中的電子郵件地址,因此需要通過Microsoft Outlook完成,但我無法弄清楚如何做到這一點。

我已經在過去使用'Hotmail'完成了這項工作,但是我無法弄清楚或者收到電子郵件給我。

我已經試過我用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("[email protected]"); 

       //Configures the address you are sending the email from 
       MailAddress address = new MailAddress("[email protected]"); 
       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("[email protected]", "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("[email protected]"); 
       // 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(); 
} 

以下

using Microsoft.Office.Interop.Outlook; 

protected void BtnSuggestPlace_Click(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); 
} 

我也有嘗試過的代碼我需要的東西通過什麼進入到我,但不知道如何或做什麼方式,

更新

我已經決定設置一個新的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("[email protected]"); 

     //Configures the address you are sending the email from 
     MailAddress address = new MailAddress("[email protected]"); 
     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 
} 
+0

什麼是例外? – Lorek

+0

@Lorek在帖子中添加了innerexception消息 – murday1983

+0

您可以ping smtp.gmail.com並查看您獲得的IP地址?它爲我解決了74.125.22.108。我收到的主機名是gmail-smtp-msa.l.google.com。此外,也許SMTP在您的網絡上被阻止。而且,僅供參考,你應該儘量讓你的例子更加緊湊。在將來,儘可能減少代碼,以便您能夠重現問題。這使其他人更容易幫助你。 – Lorek

回答

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); 
     } 
    } 
+0

我已經添加了我用於我的gmail acc的代碼,我剛剛設置了它,但它總是掉入我的'catch' 。請參閱我更新的帖子 – murday1983

0

173.194.67.109是一個有效的gmail IP:不指定端口以建立與SSL587TLS端口)的smtp連接。

我還建議使用web.config存儲SMTP設置:

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

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network 
       host="smtp.gmail.com" 
       userName="[email protected]" 
       password="YourPassword" /> 
     </smtp> 
    </mailSettings> 
</system.net> 

所以,你的代碼可能是這樣的:

Dim email As New System.Net.Mail.MailMessage() 
email.Body = "YourBody" 
email.Subject = "YourSubject" 
email.To.Add("[email protected]") 

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