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

959 Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER
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

1
On

You can use the System.Net.Mail namespace for this. You can send from any address you want.

using System.Net;
using System.Net.Mail;

using (SmtpClient client = new SmtpClient("yourserver"))
{
    client.Credential = new NetworkCredential();

    MailMessage message = new MailMessage();
    message.To.Add("targetAddress");
    message.From = new MailAddress("from addresss");
    message.Subject = "blah blah";
    message.Body = "body text.  this can be html if you want";
    message.IsBodyHtml = true; //set this to true if the body is html

    client.Send(message);
}

I've used this with Microsoft exchange and gmail and never had any trouble.