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.
Asked
Active
Viewed 932 times
2 Answers
-1
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.

Dave Greilach
- 885
- 5
- 9
-
Thanks, this is what I was looking for, the answer from user3698428 was more specific, but it's the same idea. Thanks, I will have to try to use this with our server, which uses SSL and a proxy, so I guess I will need to enter that info as well, – FelipeGuerra Jun 20 '16 at 12:58
-1
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

Ram
- 504
- 3
- 11
-
Thanks, this is what I was looking for. I will have to try to use this with our server, which uses SSL and a proxy, so I guess I will need to enter that info as well, – FelipeGuerra Jun 20 '16 at 12:59