Search code examples
c#emailoutlookspoof

Changing source e-mail address from C# program using Microsoft.Office.Interop.Outlook


I am trying to send an e-mail using a c# program I wrote to a destination company address managed by Microsoft; by default it uses my company address to send it, but I don't want my address to appear as then sender, I have tried using the "On behalf of " option, but that still shows both addresses as sender. Is there a way to change the sender without having to configure that specific sender account in my computer. I think it might not be possible because the SMTP would block the e-mail because of it being spoofed, but I hope there is a way around it. In case that's not possible is there a way to enter that e-mail credentials and SMTP server information into the C# code so I don't have to actually setup Outlook on the machine that will be sending the e-mails? Thanks in advance for the help.


Solution

  • void SendEmail(string SMTPServer, int SMTPPort, string SMTPUserName, string SMTPPassowrd, 
                  string FromEmailID, string ToEmailID, string Subject, string Body)
        {
            try
            {
                SmtpClient SmtpClient = new SmtpClient(SMTPServer, SMTPPort);
                SmtpClient.UseDefaultCredentials = false;
                SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                SmtpClient.Credentials = new System.Net.NetworkCredential(SMTPUserName, SMTPPassowrd);
                MailMessage mailMsg = new MailMessage();
                mailMsg.From = new MailAddress(FromEmailID);
                mailMsg.To.Add(ToEmailID);
                mailMsg.Subject = Subject;
                mailMsg.Body = Body;
                mailMsg.IsBodyHtml = true;
                SmtpClient.Send(mailMsg);
            }
            catch
            {
    
                throw;
            }
        }
    

    http://www.dotnetlearners.com/blogs/view/80/SMTP-send-email-source-code.aspx