我有一個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>" + " " + fldPlace.ToString()
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.ToString()
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + 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>" + " " + fldPlace.Text
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.Text
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + 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
}
什麼是例外? – Lorek
@Lorek在帖子中添加了innerexception消息 – murday1983
您可以ping smtp.gmail.com並查看您獲得的IP地址?它爲我解決了74.125.22.108。我收到的主機名是gmail-smtp-msa.l.google.com。此外,也許SMTP在您的網絡上被阻止。而且,僅供參考,你應該儘量讓你的例子更加緊湊。在將來,儘可能減少代碼,以便您能夠重現問題。這使其他人更容易幫助你。 – Lorek