简体   繁体   中英

Send Email from an Asp.Net Webform Application using Microsoft Outlook

I have an asp.net web form which on click of a button sends i want an email to be sent to my Microsoft outlook email account. I have done this on other

websites but that was using hotmail but all external web email providers are blocked by the companies firewall (as i tried setting up a gmail account) so

i need to use Outlook but i have not idea on how to implent this and solutions i have seen on Google dont seem to work. I dont know if it'll make a

difference or not but i have been informed that the users password epxires every 30days so i suspect i'll need to use windows authenication or something

but not sure.

I'm not sure on how Outlook send the email as i know from past experience from using hotmail that the email is just sent on click of the button but i'm

not sure if outlook would open the email window for the user to click the send button. If it does, i need the information captured on the webform to be

containd in the email and the content of the email body not to be altered (if this can be done, again not ot sure if it can but not a problem if it

can't).

Below is the code i used for when i tried gmail but as i said i was told it wouldnt be allowed.

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

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("MyEmailAddress@Test.co.uk");

                //Configures the address you are sending the email from
                MailAddress address = new MailAddress("EmailAddress@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.Text
                           + Environment.NewLine.ToString() +
                           "<b>" + lblLocation.Text + "</b>" + "&nbsp;" + fldLocation.Text
                           + Environment.NewLine.ToString() +
                           "<b>" + lblName.Text + "</b>" + "&nbsp;" + fldName.Text;

                //Configures the SmtpClient to send the mail
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true; //only enable this if the provider requires it

                //Setup credentials to login to the sender email address ("UserName", "Password")
                NetworkCredential credentials = new NetworkCredential("MyEmailAddress@Test.co.uk", "MyPassword");
                client.Credentials = credentials;

                //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
}

edit 2: Now we have established your going through exchance this is how my code has always worked

    SmtpClient sptmClient = new SmtpClient("exchange server name")
MailMessage m = new MailMessage();
m.To.Add(new MailAddress("Address"));
m.From = new MailAddress("");
m.Subject = "";
m.Body = "";
m.IsBodyHtml = true;
sptmClient.Send(m);

but there is another answer on here that uses outlook interoperlation that might work better for you

With Exchange it must work.

test this :

using Outlook = Microsoft.Office.Interop.Outlook;

 private void SendWithExchange()
            {

                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem mail = oApp.CreateItem(
                    Outlook.OlItemType.olMailItem) as Outlook.MailItem;
                mail.Subject = "Exemple à tester";
                Outlook.AddressEntry currentUser =
                    oApp.Session.CurrentUser.AddressEntry;
                if (currentUser.Type == "EX")
                {
                    Outlook.ExchangeUser manager =
                        currentUser.GetExchangeUser();
                    mail.Recipients.Add(manager.PrimarySmtpAddress);
                    mail.Recipients.ResolveAll();
                    //mail.Attachments.Add(@"c:\sales reports\fy06q4.xlsx",
                    //    Outlook.OlAttachmentType.olByValue, Type.Missing,
                    //    Type.Missing);
                    mail.Send();
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM