简体   繁体   中英

Send mail from outlook account using ASP.Net MVC

I have created a WebApplication which is used by my Team Members. They are accessing WebApp with a tunnel link on their PC. There is a function where Team Mates send email by clicking on button. This function is working on fine for me, but when they try using this function Outlook triggers email from my Outlook which is on my PC instead of their individual Outlook account.

Here is the code to trigger email

using (Db db = new Db())
{
Ticket newDTO = db.Tickets.Find(id);

Application app = new Application();
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "Feedback: Ticket Escalated " + newDTO.CaseId + " For Customer " + newDTO.EscalatedOn;
mailItem.To = newDTO.Email;
mailItem.CC = "lead@mydomain.com";
mailItem.HTMLBody = "Hello " + newDTO.Number + "<b>Feedback:</b>" + "<br /><br />" + newDTO.HowCanWeDeEscalate;
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
mailItem.Send();
}

How can I make this code work? So the email is sent by their individual outlook email and not mine. 

Try something like this (You need to configure the SMTP according to your server):

using System;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SO.DtProblem
{
    class Program
    {
        static async Task Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("fromemail@mydomain.com");
            mail.To.Add("toemail@mydomain.com");
            mail.CC.Add("ccemail@mydomain.com");
            mail.Subject = "test some subject";
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Body = "some body text here";


            //SMTP Conbfiguration
            string clientServer = "mail.mydomain.netORwhatever";
            string smtpUserName = "fromemail@mydomain.com";
            string smtpPassword = "somepassword";

            SmtpClient client = new SmtpClient(clientServer);
            client.Port = 25; // 587;
            client.UseDefaultCredentials = true; //false;
            client.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);
            client.EnableSsl = true; //false;

            try
            {
                await client.SendMailAsync(mail);
            }
            catch (Exception ex)
            {
                //Handle it
            }
            finally
            {
                mail.Dispose();
                client.Dispose();
            }

        }

    }
}

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