简体   繁体   中英

Sending email on behalf of other user for office365-outlook

I am sending an email using a console application created using C#.I want to send this email with a different From name , I have found the same code on many sites, but it is not working in my case.

MailAddress mailFrom = new MailAddress("xyz@abc.com", "Sender");

I am using office365- outlook.

My complete code is:-

 SmtpClient SmtpServer = new SmtpClient("smtp.office365.com", 25);
            MailAddress mailFrom = new MailAddress("xyz@abc.com", "Mailer");
            MailAddress mailTo = new MailAddress("xyz@abc.com");
            MailMessage mail = new MailMessage(mailFrom, mailTo);

            mailt.Subject = "Test Mail";
            mailt.Body = "This is for testing";

            SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@abc.com", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

So in this case is it possible to change the sender name for sending email or can we send email on behalf of other user in outlook using code?

Using the Outlook object model , you can do:

Outlook.Application OL = ....;
Outlook._MailItem mail;

mail = OL.CreateItem(Outlook.OlItemType.olMailItem) as Outlook._MailItem;
mail.Subject = subject;
mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mail.HTMLBody = body;

//  add recipients
//  ....

mail.SentOnBehalfOfName = sender;
mail.Send();

This requires Outlook to be installed on your system.

To get hints about what could be wrong with your code, you should describe the error messages or failure you are getting.

你可以做:

    mail.From = new MailAddress("test@example.com");

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